React-Native: when do I need to switch to a state of reduction and react to state in an application?

I applied redux in my application. My scenario is a transition from one component to another, I am updating the state of redux . Therefore, when I return to the previous component, I get the latest redux state redux .

Expected Result:

When I return to the previous component, you should be in the same state. I do not need the latest change data.

 Ex: Marie ->April, when I go back, Marie <- April 

Actual result:

Updating the latest change data.

 Ex: Marie ->April, when I go back, April <- April 

How can I write an application structure for this? Does it redux here? or Should I manage along with the state of the component, passing as params when navigating?

+5
source share
3 answers

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.

+2
source

Good to know what to use when.

Duration

Different parts of the state persist for different times. I categorize the categorization of time in these groups:

Short-term: data that changes quickly in your application - keep this data type local to React

Mid-term: data that is likely to persist for some time in your application - save state in Redux repository

Long-term: data that should last between several visits to your site is stored somewhere else, probably in a database on the server or in local storage

check here for more information: React State vs Redux State When and why?

+2
source

If data is required in more than one class, then use the relx else condition to use the state of the reaction, it depends on the component life cycle. Since the state of redux is global, and the state of reaction is active only for the class in which it is defined. According to your condition, you can use the redux state for the first component, since you will return to it, and you want Marie to be your data and use the reaction state for the second component, since you want April to be your data.

+1
source

Source: https://habr.com/ru/post/1272446/


All Articles