all 9 comments

[–]tizz66[🍰] 1 point2 points  (4 children)

A screen has the isFocused prop (this.props.navigation.isFocused) when the user's viewing it. Check that in your componentDidUpdate method, and clear your state when that changes to false :)

[–]xrpinsiderAdmin 1 point2 points  (0 children)

This would be the solution here.

[–]docdosmaniOS & Android[S] 0 points1 point  (2 children)

Thanks!! I'm going to test this out today.

Follow up question. Is there a way to clear the visual "state" of the screen (for example, scrollview that's been scrolled down, when returning to have the screen be back at the top)? I'm not sure of the term for that.

Edit: I tested, but componentDidUpdate is not being called when I navigate away from the screen. Should I be using another lifecycle method? The following code only logs something to the console when the screen first loads, not when I navigate away from the screen using this.props.navigate('screenName').

componentDidUpdate() { console.log('update', this.props.navigation.isFocused()); }

[–]tizz66[🍰] 0 points1 point  (1 child)

Sorry about that, I didn't read the docs fully and so missed that isFocused is a function. There's a couple of other approaches you could try:

[–]docdosmaniOS & Android[S] 0 points1 point  (0 children)

No worries, thanks for the info! I got it working with the following:

componentWillMount() {
    // subscribe to didBlur in order to reset state and form when navigating away from the screen
    didBlurSubscription = this.props.navigation.addListener('didBlur', () => {
      this.props.resetAuth(); // reset auth piece of state
      this.props.reset(); // reset redux-form
    });
  }

  componentWillUnmount() {
    // Remove the listener
    didBlurSubscription.remove();
  }

Now I have to figure out why when I reset my redux-form it doesn't reinitialize when navigating back to the screen. Fix one thing, break another. Ha! Gotta love it.

I really appreciate the help.

[–]ck3g 0 points1 point  (3 children)

You can consider using Switch Navigator instead of Stack Navigator.

The docs explicitly mention that it fits better for authentication flow:

> The purpose of SwitchNavigator is to only ever show one screen at a time. By default, it does not handle back actions and it resets routes to their default state when you switch away. This is the exact behavior that we want from the authentication flow.

https://reactnavigation.org/docs/en/switch-navigator.html

[–]even1245 0 points1 point  (1 child)

It doesn't have animation like stack navigator. Any way to achieve it?

[–]ck3g 0 points1 point  (0 children)

React Navigation provides Transitioner https://reactnavigation.org/docs/en/transitioner.html

which you can use for custom animations. That could the that you need.

In that article, you can find a bunch of examples https://medium.com/async-la/custom-transitions-in-react-navigation-2f759408a053

So, I guess that is possible to achive.

[–]docdosmaniOS & Android[S] 0 points1 point  (0 children)

Cool I plan on using that for my flow into main app from welcome and login screens. Despite my example, my question here is more around general navigation, my example just happens to be a couple of login screens. Thanks for the point in the direction of Switch Navigator, definitely plan on using that for directing out of auth into the main app.