Skip to content

Commit 7dac240

Browse files
committed
pass data from page via Link
1 parent 2c8a415 commit 7dac240

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/components/misc/NestedRoutes.jsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import React from 'react';
2-
import { Link, Route, useParams, useRouteMatch } from 'react-router-dom';
2+
import {
3+
Link,
4+
Route,
5+
useParams,
6+
useRouteMatch,
7+
useLocation,
8+
} from 'react-router-dom';
39

410
const Item = () => {
511
const { name } = useParams();
@@ -13,10 +19,15 @@ const Item = () => {
1319

1420
const NestedRoutes = () => {
1521
const { url, path } = useRouteMatch();
22+
const location = useLocation();
1623
console.log(useRouteMatch());
1724

1825
return (
1926
<div>
27+
<p> Getting Data from the Link of Prev Page</p>
28+
<p>Name: {location.state.name}</p>
29+
<p>Msg: {location.state.msg}</p>
30+
2031
<ul>
2132
<li>
2233
<Link to={`${url}/shoes`}>Shoes</Link>
@@ -28,7 +39,7 @@ const NestedRoutes = () => {
2839
<Link to={`${url}/footwear`}>Footwear</Link>
2940
</li>
3041
</ul>
31-
42+
3243
<Route path={`${path}/:name`}>
3344
<Item />
3445
</Route>

src/components/misc/PageLinks.jsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import { Link } from 'react-router-dom';
44
const PageLinks = () => {
55
return (
66
<>
7+
<Link to="/ui">UI</Link>
8+
79
<Link
810
to={{
9-
pathname: '/ui',
10-
data: { name: 'nish', msg: 'Hi people' },
11+
pathname: '/nested',
12+
state: { name: 'nish', msg: 'Hi people' },
1113
}}
1214
>
13-
Send data to route
15+
Nested Routes
1416
</Link>
1517

16-
<Link to="/nested">Nested Routes</Link>
17-
1818
<Link
1919
to={{
2020
pathname: '/rhform',
@@ -46,7 +46,7 @@ const PageLinks = () => {
4646
>
4747
Check Graphql fetch
4848
</Link>
49-
49+
5050
<Link
5151
to={{
5252
pathname: '/casl',

0 commit comments

Comments
 (0)