I've been following the documentation here from react-router-dom's Redirect. Right now, I have a component like that renders the following:
return <Redirect to={{ pathname: '/login', state: { previousPath: path } }} />;
However, I'm not sure how to access previousPath
in state at my new path. The documentation is not clear on this. How do I access previousPath
?
One of your parent components should be
Route
with props: match, location, history. You should be able to use the state you created withprops.location.state
to access yourpreviousPath
value.See location in the React Router docs.