I figured out the way how to fix it. So, when I use anchor elements for navigating, the app reloads so the states reloads too. But with internal links like @reach/router and gatsby-link, the app doesn't reload so the state doesn't reload as well. My solution is to use the useLocation hook from @reach/router and put it in the useEffect's deps, like:
I figured out the way how to fix it. So, when I use anchor elements for navigating, the app reloads so the states reloads too. But with internal links like
@reach/router
andgatsby-link
, the app doesn't reload so the state doesn't reload as well. My solution is to use theuseLocation
hook from@reach/router
and put it in theuseEffect
's deps, like:It will update the state whenever the route is changed.
I still have no idea why your sandbox is still working. But thank you for your response. Keep up with great contents like this one!