I'm not sure my answer is the right way to implement it. But currently I am doing it like this:
State component can be scattered across multiple locations (ex: this.state , class fields, global object, or Redux).
What I understand from your question is that you are dealing with two requirements at the same time:
- Part of the state of the component should be preserved when moving backward and ford.
- Another part of the state of the component must be shared with other components (remain unchanged) when navigating back and forth.
I do not know a single solution for both requirements. If I have to do this, I will construct the state of the component so that it is distributed or scattered at different addresses, each of which performs its own tasks. Basically, I use 2 different solutions:
- The part of the state that needs to be saved will be placed in
this.state . - The part of the state that needs to be shared will be placed in
Redux .
For this solution, I am not using react-redux , only Redux . In Redux there is a store.getState () method that will return a single Redux state object. I make the store read-only globally and use it to render parts of the component that need shared data.
source share