Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rerender router based on boolean auth flag #3063

Closed
thedc89 opened this issue Jun 1, 2018 · 1 comment
Closed

Rerender router based on boolean auth flag #3063

thedc89 opened this issue Jun 1, 2018 · 1 comment

Comments

@thedc89
Copy link

thedc89 commented Jun 1, 2018

I would like to switch between Scenes on authentication. I used the approach shown in this issue:
#913
but it does not rerender when I sign in/ sign out
I'd like to rerender my Router when I pass props to change the screen, not sure though whether it is possible this way

My router:

class RouterComponent extends Component {
  componentWillReceiveProps(nextProps) {
    this.forceUpdate();
  }
  render() {
    return (
      <Router navigationBarStyle={styles.navBar} navBarButtonColor='#fff'>
        <Scene key='root' hideNavBar modal>
          <Scene key='auth' initial={!this.props.isUserSignedIn}>
.
.
.

My App.js

class App extends Component {

  constructor() {
    super();
    this.state = {
      loading: true,
    };
  }

  componentWillMount() {
    this.authSubscription = firebase.auth().onAuthStateChanged((user) => {
      this.setState({
        loading: false,
        user
      });
    });
  }


  render() {
    if (this.state.loading) return null;
    return (
      <Provider store={store}>
      <Router isUserSignedIn={this.state.user !== null} />
      </Provider>
    );
  }
}

export default App;

Although it works well once app is launched, it does not change the scene when the flag changed down the road, etc when user signs in/ signs out.

It looks like, despite the fact that render() function is being called with the correct props, the router remains the same, is it some intrinsic feature of Router that its root is rendered only once on launch?

@aksonov
Copy link
Owner

aksonov commented Jun 6, 2018

You may check https://github.com/aksonov/react-native-starter-app for some authentication ideas. v4 supports on, success, failure props that allows to act like the state machine.

@aksonov aksonov closed this as completed Jun 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants