Skip to content

Commit

Permalink
docs(gatsby-link): add example of passing state (gatsbyjs#10137)
Browse files Browse the repository at this point in the history
Update docs to show how state is passed through the location object.

<!--
  Q. Which branch should I use for my pull request?
  A. Use `master` branch (probably).

  Q. Which branch if my change is a bug fix for Gatsby v1?
  A. In this case, you should use the `v1` branch

  Q. Which branch if I'm still not sure?
  A. Use `master` branch. Ask in the PR if you're not sure and a Gatsby maintainer will be happy to help :)

  Note: We will only accept bug fixes for Gatsby v1. New features should be added to Gatsby v2.

  Learn more about contributing: https://www.gatsbyjs.org/docs/how-to-contribute/
-->
  • Loading branch information
GarrettJMU authored and gpetrioli committed Jan 22, 2019
1 parent b2f4514 commit 179296d
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions docs/docs/gatsby-link.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,41 @@ render () {
}
```

You can also pass state to pages when you navigate e.g. `navigate("/a-path/", { state: { pleasant: "reasonably" }}`

Note that `navigate` was previously named `navigateTo`. `navigateTo` is deprecated in Gatsby v2.

## Passing state through Link and Navigate

You can pass state to pages when you navigate, such as:

```javascript
navigate(`/a-path/`, { state: { pleasant: `reasonably` }}
```
You can also pass state to pages when you use `Link`:
```jsx
<Link
to="/another-page/"
activeStyle={{
color: "red",
}}
state={{
pleasant: "reasonably",
}}
>
```
This is accessible from the `location` object on the new page:
```javascript
componentDidMount() {
const pleasant = this.props.location.state.pleasant
this.setState({
pleasant: pleasant
})
}
```
## Prefixed paths helper
It is common to host sites in a sub-directory of a site. Gatsby lets you [set
Expand Down

0 comments on commit 179296d

Please sign in to comment.