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:
Hmm, I was unable to reproduce it for
@reach/routerHere is the sandbox: codesandbox.io/s/reach-router-curs...
Can you provide more details? 🙂
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/routerandgatsby-link, the app doesn't reload so the state doesn't reload as well. My solution is to use theuseLocationhook from@reach/routerand 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!