Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing <Link>'s underlying node via innerRef #5294

Merged
merged 3 commits into from
Jun 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/react-router-dom/docs/api/Link.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,16 @@ When `true`, clicking the link will replace the current entry in the history sta
```js
<Link to="/courses" replace />
```

## innerRef: function

Allows access to the underlying `ref` of the component

```js

const refCallback = node => {
// `node` refers to the mounted DOM element or null when unmounted
}

<Link to="/" innerRef={refCallback} />
```
4 changes: 2 additions & 2 deletions packages/react-router-dom/modules/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ class Link extends React.Component {
}

render() {
const { replace, to, ...props } = this.props // eslint-disable-line no-unused-vars
const { replace, to, innerRef, ...props } = this.props // eslint-disable-line no-unused-vars

const href = this.context.router.history.createHref(
typeof to === 'string' ? { pathname: to } : to
)

return <a {...props} onClick={this.handleClick} href={href}/>
return <a {...props} onClick={this.handleClick} href={href} ref={innerRef}/>
}
}

Expand Down
16 changes: 16 additions & 0 deletions packages/react-router-dom/modules/__tests__/Link-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ describe('A <Link>', () => {

expect(href).toEqual('/the/path?the=query#the-hash')
})

it('exposes its ref via an innerRef prop', done => {
const node = document.createElement('div')

const refCallback = n => {
expect(n.tagName).toEqual('A')
done()
}

ReactDOM.render(
<MemoryRouter>
<Link to="/" innerRef={refCallback}>link</Link>
</MemoryRouter>,
node
)
})
})

describe('When a <Link> is clicked', () => {
Expand Down