You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/upgrading/v5.md
+26-11Lines changed: 26 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -560,6 +560,31 @@ function App() {
560
560
}
561
561
```
562
562
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
+
functionHome() {
580
+
constlocation=useLocation();
581
+
conststate=location.state;
582
+
return (
583
+
<div>Home</div>
584
+
);
585
+
}
586
+
```
587
+
563
588
## Use `useRoutes` instead of `react-router-config`
564
589
565
590
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() {
636
661
}
637
662
```
638
663
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.
650
665
651
666
If you prefer to use a declarative API for navigation (ala v5's `Redirect` component), v6 provides a `Navigate` component. Use it like:
0 commit comments