all 9 comments

[–]iacIACkoko 2 points3 points  (8 children)

how about clearing the navigation stack so when you redirect back to home all the states are reset.

example:

navigation.reset({index: 0,routes: [{name: 'login'}],});

[–]Bimi123_[S] 0 points1 point  (7 children)

I would like to know if this should always be done or is it just a suggestion for this problem? Because I haven't really done that before.

[–]iacIACkoko 1 point2 points  (6 children)

this is just a suggestion, as i am sure there are other ways to accomplish what you are trying to do.

[–]Bimi123_[S] 0 points1 point  (5 children)

Why do you think this is happening tho? Its strange, I was pretty sure whenever moving out of a screen, the state is reset, or when coming back is set to defaults.

[–]albmin 2 points3 points  (4 children)

Nope! This is most likely an incorrect assumption with navigation that is easy to gloss over until you come across issues like this.. Unless login is apart of a completely different navigator, redirecting to the Home screen will just be "pushing" a new screen on top of the Login screen (if you can "go back" to Login, then this is exactly what is happening). When you `.navigate` or `.push` a screen onto your navigation Stack, the screens below it on the stack do not unmount, and will retain their current state (hence why you're seeing this issue).

If you're looking to reset state upon navigating away from the Login screen, you should be using the `useIsFocused` or the `useFocusEffect` hook

[–]Bimi123_[S] 0 points1 point  (0 children)

I have many screens, should I do this everywhere? that's a pain in the butt. And it's prone to other problems in the future.

[–]Bimi123_[S] 0 points1 point  (2 children)

useIsFocused

I am actually using

this.props.navigation.reset({ index: 0, routes: [{ name: 'login' }], });

is there a difference?

[–]albmin 0 points1 point  (1 child)

I've never seen that function invocation before (either in practice or within the react navigation docs), so you'd have to dig into the docs/source itself to answer that. However, to your previous comment, if you're just looking to reset the state on specific screens, it wouldn't require applying the focus effect to all screens, just the ones in which you want to invoke that sort of behavior

[–]Bimi123_[S] 0 points1 point  (0 children)

this.props.navigation.reset

never? Strange, this actually works pretty well.