From 992cd53c57206df52c11e69543580e1ec3e3b4e6 Mon Sep 17 00:00:00 2001 From: Garrett Hughes Date: Wed, 28 Nov 2018 04:05:38 -0800 Subject: [PATCH] docs(gatsby-link): add example of passing state (#10137) Update docs to show how state is passed through the location object. --- docs/docs/gatsby-link.md | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/docs/docs/gatsby-link.md b/docs/docs/gatsby-link.md index 33a479b629d79..0f53e8cf4c846 100644 --- a/docs/docs/gatsby-link.md +++ b/docs/docs/gatsby-link.md @@ -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 + +``` + +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