Skip to content

Commit d8937d0

Browse files
ptimwardpeet
andauthored
fix(gatsby-link): Adds Type, PropType, tests, and docs for `st… (#20807)
* Fix code example for "Example of providing state to a link component" https://www.gatsbyjs.org/docs/location-data-from-props/#example-of-providing-state-to-a-link-component Signed-off-by: Tim Osborn <[email protected]> * Add PropType and TS definition for the `state` prop Signed-off-by: Tim Osborn <[email protected]> * Cross link docs relevant to GatsbyLink Signed-off-by: Tim Osborn <[email protected]> * Add tests for passing state Signed-off-by: Tim Osborn <[email protected]> * Update index.d.ts Co-authored-by: Ward Peeters <[email protected]>
1 parent 55f99d1 commit d8937d0

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

docs/docs/gatsby-link.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ const Form = () => (
249249
)
250250
```
251251

252+
Then from the receiving page you can access the `location` state as demonstrated in [Pass state as props to the linked page](#pass-state-as-props-to-the-linked-page).
253+
252254
### Replace history during programmatic navigation
253255

254256
If the navigation should replace history instead of pushing a new entry into the navigation history, add the `replace` prop with a value of `true` to the `options` argument of `navigate`.
@@ -390,4 +392,5 @@ onClick = () => {
390392
## Additional resources
391393

392394
- [Authentication tutorial for client-only routes](/tutorial/authentication-tutorial/)
395+
- [Routing: Getting Location Data from Props](/docs/location-data-from-props/)
393396
- [`gatsby-plugin-catch-links`](https://www.gatsbyjs.org/packages/gatsby-plugin-catch-links/) to automatically intercept local links in Markdown files for `gatsby-link` like behavior

docs/docs/location-data-from-props.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ Through client-side routing in Gatsby you can provide a location object instead
3838
<Link to="/somepagecomponent"/>
3939

4040
// but if you want to add some additional state
41-
<Link to={{
42-
pathname: '/somepagecomponent',
43-
state: {modal: true}
41+
<Link
42+
to={'/somepagecomponent'}
43+
state={{modal: true}}
4444
}}>
4545
```
4646

@@ -64,6 +64,7 @@ The great thing is you can expect the `location` prop to be available to you on
6464
6565
## Other resources
6666
67+
- [Gatsby Link API](/docs/gatsby-link/)
6768
- [@reach/router docs](https://reach.tech/router/api/Location)
6869
- [react-router location docs](https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/location.md)
6970
- [Hash Router](https://reacttraining.com/react-router/web/api/HashRouter)

packages/gatsby-link/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export interface GatsbyLinkProps<TState> extends LinkProps<TState> {
1212
partiallyActive?: boolean
1313
/** Used to declare that this link replaces the current URL in history with the target */
1414
replace?: boolean
15+
/** Used to pass state data to the linked page.
16+
* The linked page will have a `location` prop containing a nested `state` object structure containing the passed data.
17+
*/
18+
state?: TState
1519
/** The URL you want to link to */
1620
to: string
1721
}

packages/gatsby-link/src/__tests__/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,18 @@ describe(`navigate`, () => {
227227
undefined
228228
)
229229
})
230+
231+
it(`passes a state object`, () => {
232+
const to = `/some-path`
233+
const options = { state: { myStateKey: `a state value` } }
234+
235+
getNavigate()(to, options)
236+
237+
expect(global.___navigate).toHaveBeenCalledWith(
238+
`${global.__BASE_PATH__}${to}`,
239+
options
240+
)
241+
})
230242
})
231243

232244
describe(`ref forwarding`, () => {
@@ -253,3 +265,18 @@ describe(`ref forwarding`, () => {
253265
expect(ref.current).toEqual(expect.any(HTMLElement))
254266
})
255267
})
268+
269+
describe(`state`, () => {
270+
it(`passes a state object`, () => {
271+
const to = `/`
272+
const state = { myStateKey: `a state value` }
273+
getNavigate()
274+
275+
const { link } = setup({ linkProps: { state } })
276+
link.click()
277+
278+
expect(
279+
global.___navigate
280+
).toHaveBeenCalledWith(`${global.__BASE_PATH__}${to}`, { state })
281+
})
282+
})

packages/gatsby-link/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ GatsbyLink.propTypes = {
187187
onClick: PropTypes.func,
188188
to: PropTypes.string.isRequired,
189189
replace: PropTypes.bool,
190+
state: PropTypes.object,
190191
}
191192

192193
const showDeprecationWarning = (functionName, altFunctionName, version) =>

0 commit comments

Comments
 (0)