I’m having a need to implement the following behavior in my React application:
- Whenever a page is loaded (either opened in a browser or refreshed), certain components must retrieve parameters from a query string.
- React application should be able to modify the query string without reloading a page.
- Whenever a component retrieving the query string on initial loading / refreshing of a page vanishes but appears again (for example, whenever a tab is switched), it is considered its first render, but it should not provoke additional retrieving of the query string’s parameters.
I have tried:
- Using
useEffectwith empty array on target component(-s). Didn’t work well because it was triggering every time I changed a tab (the disappearance of any component is considered its unmount, and its appearance after that is considered a mount, leading to the satisfied condition for auseEffectrunning on initial render). - Using Context API to store the query string in
index.jsfile of an application, and retrieving the context of that query string by target component(-s). Didn’t work due to the same reason:useContextdid re-run on re-rendering.
Is there a pattern people use in React with React-Router v6 to achieve this?