I'm struggling to pass some data from one component to another one using <Link />. I cannot pass any params to my URL so, please excuse that solution. Below are my sample files from my problem,
App.jsx
<BrowserRouter>
<Router history={history}>
<Route exact path={'/my-dashboard/first-page'} component={FirstComponent}/>
</Router>
</BrowserRouter>
LinkingComponent.jsx
const LinkingComponent = ({ data }) => {
return (
<Link
to={{
pathname: '/my-dashboard/first-page',
state: { userDetails: data }
}}
>
{data.firstName}
{data.lastName}
</Link>
);
};
FirstComponent.jsx
const FirstComponent = props => {
const {
location: {
state: { userDetails }
}
} = props;
console.log('user-detail', userDetails);
....
some data manipulations
....
}
Here, the FirstComponent is not directly linked or related to the LinkingComponent. I'm using the LinkingComponent in my dashboard and I am passing my 'data' there. It is receiving it properly in the LinkingComponent. But somehow whenever I try to access it in my FirstComponent, it gives me an error that it is undefined. I have already tried various solutions and read various articles, but couldn't find an appropriate solution.
Please help me with the solution here. TIA.