-
This might be a Rust question but I am new enough to all this I cannot say for sure. I create an application like this:
I then add a route to get back to the counter page:
If I increment the counter, then go to another page and then back the counter is reset to zero. I would like it to retain the count. Of course The section in the book on Global State Management indicates "So far, we've only been working with local state in components". However, I don't see that in a way that makes sense to me. The section on The Life Cycle of a Signal makes it clear components are just function calls which makes me think some kind of static(?) variable is needed. Any pointers as to how I should address this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
So this is working as designed and the way any reasonable framework would work: when you navigate away from the page, it releases any memory allocated on that page (rather than just leaking memory as you navigate page to page). A good way to share state between different pages would be to "hoist" it up into a higher scope, and then share it with the child. In your example, you would pull the count signal up into the scope of the App component instead of the page it's on, so that it is created once during the life of the app: then pass it as a prop or using context. Note that for the Route view prop, |
Beta Was this translation helpful? Give feedback.
So this is working as designed and the way any reasonable framework would work: when you navigate away from the page, it releases any memory allocated on that page (rather than just leaking memory as you navigate page to page).
A good way to share state between different pages would be to "hoist" it up into a higher scope, and then share it with the child.
In your example, you would pull the count signal up into the scope of the App component instead of the page it's on, so that it is created once during the life of the app: then pass it as a prop or using context.
Note that for the Route view prop,
view
can be any function including a closure, so you can do something likeview=move || vi…