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

fix: enable ref forwarding with forwardRef #9892

Merged
merged 3 commits into from
Jan 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions docs/docs/gatsby-link.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Preloading is triggered by a link entering the viewport; Gatsby uses
`Link`'s `innerRef` property to create a new IntersectionObserver (on
supported browsers) to monitor visible links. This way, Gatsby only prefetches
code/data chunks for pages the user is likely to navigate to. You can also get
access to the link element by passing in a `innerRef` prop.
access to the link element by passing in a `ref` prop, which will be forwarded to the `@reach/router` `Link` element directly.

## How to use

Expand All @@ -39,7 +39,7 @@ class Page extends React.Component {
activeStyle={{
color: "red",
}}
innerRef={el => {
ref={el => {
this.myLink = el
}}
state={{
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby-link/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"license": "MIT",
"main": "index.js",
"peerDependencies": {
"gatsby": ">2.0.0-alpha"
"gatsby": ">2.0.0",
"react": ">=16.3"
},
"repository": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-link",
"scripts": {
Expand Down
18 changes: 18 additions & 0 deletions packages/gatsby-link/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,21 @@ describe(`navigate`, () => {
)
})
})

describe(`ref forwarding`, () => {
it(`forwards ref`, () => {
const ref = jest.fn()
setup({ linkProps: { ref } })

expect(ref).toHaveBeenCalledTimes(1)
expect(ref).toHaveBeenCalledWith(expect.any(HTMLElement))
})

it(`remains backwards compatible with innerRef`, () => {
const innerRef = jest.fn()
setup({ linkProps: { innerRef } })

expect(innerRef).toHaveBeenCalledTimes(1)
expect(innerRef).toHaveBeenCalledWith(expect.any(HTMLElement))
})
})
7 changes: 4 additions & 3 deletions packages/gatsby-link/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const handleIntersection = (el, cb) => {

class GatsbyLink extends React.Component {
constructor(props) {
super()
super(props)
// Default to no support for IntersectionObserver
let IOSupported = false
if (typeof window !== `undefined` && window.IntersectionObserver) {
Expand Down Expand Up @@ -97,7 +97,6 @@ class GatsbyLink extends React.Component {
/* eslint-disable no-unused-vars */
activeClassName: $activeClassName,
activeStyle: $activeStyle,
ref: $ref,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LekoArts this fixes the bug re: accessing ref.

innerRef: $innerRef,
state,
replace,
Expand Down Expand Up @@ -154,7 +153,9 @@ GatsbyLink.propTypes = {
replace: PropTypes.bool,
}

export default GatsbyLink
export default React.forwardRef((props, ref) => (
<GatsbyLink innerRef={ref} {...props} />
))

export const navigate = (to, options) => {
window.___navigate(withPrefix(to), options)
Expand Down