How do I access the last path in the current component? (React router V6)

Viewed 57

I would like to render the UI differently depending on where we arrive at the current page. For example, an item detail page should have an extra 'delete' button if we come from '/mycollection' versus '/apiSearchResults'. How do I achieve this in react router v6?

I know I can create an Higher order component that encapsulates the reused component and the extra feature. But I am wondering if I can do without an HOC.

Thanks!

1 Answers

You can pass down state using the <Link> component, in this case a pathname:

<Link to={{pathname: '/nextpath', state: { prevPath: location.pathname }}}>Example Link</Link>

You can then access prevPath from this.props.location.state in the next component

Related