question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Global State Does Not Seem To Persist Between Page Routes When using React Router.

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
wibedcommented, Oct 14, 2020

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

1reaction
wibedcommented, Oct 14, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found