useState always using initial value on route change #131
-
When moving back (or forward) in the history, I would like the component state to be the same as it was before the route change. I was under the impression it would remember (without resorting to something like a global store) because the initial value would only apply for the component's initial render. Am I mistaken? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
It should. This is likely a bug. Can you send me a CodeSandBox?
…On Nov 25, 2021, 8:49 PM -0700, Yak ***@***.***>, wrote:
When moving back (or forward) in the history, I would like the component state to be the same as it was before the route change. I was under the impression it would remember (without resorting to something like a global store) because the initial value would only apply for the component's initial render. Am I mistaken?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.
|
Beta Was this translation helpful? Give feedback.
-
Sorry for the delay. Here is a CodeSandBox to display its behaviour - I noticed it in my app but I just forked your simple example and added a counter in This is using hash history:
Perhaps this is related to using hash history? |
Beta Was this translation helpful? Give feedback.
-
Sorry, I think we misunderstood each other. In your example, the Sorry it took me a bit to figure out what you were referring to. |
Beta Was this translation helpful? Give feedback.
Sorry, I think we misunderstood each other. In your example, the
useState
is being unmounted as you transition to the individual post view. It's not rendered any more, so it's memory is thrown away. This is how React works at a fundamental level. You are 100% correct that you cannot persist component state without some type of external storage (global store, server store, URL store).Sorry it took me a bit to figure out what you were referring to.