'how to get `props.location.state` after first render of the linked component?

I have two components products and partners, there is link in products that looks like this:

<Link to={{
            pathname:"/partners",
            state: { from: props.location.pathname },
        }}>
    To Partners
</Link>

now in the partners if I do:

const location = useLocation();
const from = location?.state?.from;
console.log(from)

on initial render I get the correct output /products, but the problem is that partners component rerenders multiple time not just once so I get undefined after the initial render, so my question is how to overcome this problem?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source