differentiate navigation due to router.navigate vs browser navigation
Is there any way to determine if searchParams change due to my own call of router.navigate vs the user/browser navigating with the forward/back buttons for example?
context:
im trying to have my app state represented in the url, and also allow the user to navigate between urls with different search params and load the state appropriately.
the issue im running into is i have a useEffect depending on the searchParams that updates state.
I also have a useEffect depending on state that updates the url. These can get into a loop. Even with some early returns from the useEffects to prevent a loop, i cant get a set up that properly achieves the above goal. If i could reliably determine that searchParams changed due to a browser action rather than my own, then i could make each useEffect only run at the correct time.
3 Replies
like-goldOP•4mo ago
Or in general, does the router provide a way to have two way binding between app state and search params
to add to the op, the specific issue with my current setup is that, when navigating with browser buttons, the useEffect that is meant to react to state changes is triggering, causing it to update the url which undoes the navigation just done by the browser.
this issue doesnt occur on intial page load, since i can detect that it is onMount and not update the url
if i could access some kind of equivalent thing to onMount that is only true for the first render after a browser navigation, then i could also solve the problem
metropolitan-bronze•4mo ago
Could you show us a bit of code? I don't think what you are asking for exists, but maybe we could help you find a way around it.
Probably the ideal solution would involve not having to do this:
i have a useEffect depending on the searchParams that updates state. I also have a useEffect depending on state that updates the url
like-goldOP•4mo ago
i was able to get it working through very careful handling of what dependencies the useEffects had and converting certain things to refs to prevent them running unnecessarily