Does the value in this.setState({value:true;}) be available even after navigating across pages

- 1 answer

Ad

This is in regard to React-Native in particular, but, its the same in ReactJS as well I guess, so any answers would be great.

If I set a state in Page 1, say-

this.setState({this.state.value:true});

If I navigate to another scene/page, say Page 2 using the Navigator component. And say I come back or in later point of time navigate back to Page 1, Can I still be able to access-

if(this.state.value())
  console.log("True");
else
  console.log("False");

I am not able to get to this stage as well. So, wanted to know if its possible. If not, how do I persist some properties of that page(say checkbox values, radio buttons values, some True/False flags etc.,) Is AsyncStorage the only other way?

Adding more thoughts:

This.prop would be more like a property that we have to pass as parameters to child modules right? But, what I need is - data to persist in the current page. Giving an example - An applist implementation(in case of react native) Android. Only when we launch the applist screen I want to see the applist and its data.

Ad

Answer

Ad

Yes the state remains for the page as long as you don't reinitialize the page. Meaning you can't call the same page again farther down in the navigation and expect it to keep it's state. You would then need to use this.prop.

I see a couple of issues. To set the state you do it like this: this.setState({foo: true});

Then you can see the result here:

if (this.state.foo) { // <-- you were calling value() as a function...
    console.log("True");
} else {
    console.log("False");
}
Ad
source: stackoverflow.com
Ad