Global State Does Not Seem To Persist Between Page Routes When using React Router.
See original GitHub issueI can’t seem to hold on to a global state value using useTrackedState(), it seems like when a React Router <Route>
component is used, the state is re-initialized. This could just be an issue with my application architecture, but I’d like some feedback none the less…
Here’s an example test application from my personal GitHub repository: GitHub:react-tracked-test.
Thanks for any help you can provide.
Issue Analytics
- State:
- Created 4 years ago
- Comments:16 (8 by maintainers)
Top Results From Across the Web
5 Methods to Persisting State Between Page Reloads in React
One of the straightforward options is to use localStorage in the browser to persist the state. Let's take a look at an example....
Read more >How to save states between route change in React
First set up a global state manager. you can use redux, or context API. I prefer to use zustand ...
Read more >React Router with Redux: Understanding navigation state
Use React Router to declaratively navigate within your React and Redux applications and maintain state across your app's navigation ...
Read more >Strict Mode - React
StrictMode is a tool for highlighting potential problems in an application. Like Fragment , StrictMode does not render any visible UI.
Read more >How To Handle Routing in React Apps with React Router
The <h1> tag is going to serve as a global page title. Since you want it to appear on every page, configure the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
ill test it right away
EDIT: alright, I tried both approaches using reducers and state and I couldn’t manage to get the state to persist
EDIT II: ive implement a purely useContext state management system and are facing the same issues
EDIT III: I tried again. and this time I put the router element on the correct place. it seems my links have defaulted to a full page reload instead of navigating in-between components which caused the state to reset. @dai-shi thank you very much for providing so much help
I tested it once more using the
useState
pattern as seen in example 07. ❤️ works as expected.