Skip to content

Commit cc67744

Browse files
authored
Emphasize <Link> state changes (#9954)
1 parent 1304412 commit cc67744

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- awreese
1717
- aymanemadidi
1818
- bavardage
19+
- bbrowning918
1920
- BDomzalski
2021
- bhbs
2122
- bobziroll

docs/upgrading/v5.md

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,31 @@ function App() {
560560
}
561561
```
562562

563+
## Pass `<Link>` state as separate prop
564+
565+
The `Link` component in v6 accepts `state` as a separate prop instead of receiving it as part of the object passed to `to` so you'll need to update your `Link` components if they are using `state`:
566+
567+
```js
568+
import { Link } from "react-router-dom";
569+
570+
// Change this:
571+
<Link to={{ pathname: "/home", state: state }} />
572+
573+
// to this:
574+
<Link to="/home" state={state} />
575+
```
576+
577+
The state value is still retrieved in the linked component using `useLocation()`:
578+
```js
579+
function Home() {
580+
const location = useLocation();
581+
const state = location.state;
582+
return (
583+
<div>Home</div>
584+
);
585+
}
586+
```
587+
563588
## Use `useRoutes` instead of `react-router-config`
564589

565590
All of the functionality from v5's `react-router-config` package has moved into core in v6. If you prefer/need to define your routes as JavaScript objects instead of using React elements, you're going to love this.
@@ -636,17 +661,7 @@ function App() {
636661
}
637662
```
638663

639-
If you need to replace the current location instead of push a new one onto the history stack, use `navigate(to, { replace: true })`. If you need state, use `navigate(to, { state })`. You can think of the first argument to `navigate` as your `<Link to>` and the other arguments as the `replace` and `state` props. The `Link` component in v6 accepts `state` as a separate prop instead of receiving it as part of the object passed to `to` so you'll need to update your `Link` components if they are using `state`:
640-
641-
```js
642-
import { Link } from "react-router-dom";
643-
644-
// Change this:
645-
<Link to={{ pathname: "/home", state: state }} />
646-
647-
// to this:
648-
<Link to="/home" state={state} />
649-
```
664+
If you need to replace the current location instead of push a new one onto the history stack, use `navigate(to, { replace: true })`. If you need state, use `navigate(to, { state })`. You can think of the first argument to `navigate` as your `<Link to>` and the other arguments as the `replace` and `state` props.
650665

651666
If you prefer to use a declarative API for navigation (ala v5's `Redirect` component), v6 provides a `Navigate` component. Use it like:
652667

0 commit comments

Comments
 (0)