-1

I'm using React Router to handle routing in my app, and I need to pass data from the route to the component being rendered. I'm not sure how to access the data in the component.

I have tried using the useEffect hook to store the data in local storage, but I'm not sure how to retrieve the data and update the state when the component mounts.

I expect to be able to persist data across components using React Hooks.

Ali Raza
  • 1
  • 2

1 Answers1

0

you can pass the data as a prop of the component.

<Route path="<your_path>" element={<Component data ={value} />} />

or else you can use dynamic routes to pass the data in URL and then get the value using useParams() hook.

<Route path="<your_path>/:<dynamic_value>" element={<Component />} />

In component.js

import {useParams} from 'react-router-dom';
function Component(){
const {<dynamic_value>} = useParams();
---rest of your logic---
}
Deeptiranjan
  • 30
  • 1
  • 6