简体   繁体   English

在React Native应用程序中加载主屏幕之前显示模式

[英]Show modal before main screen loads in react-native app

I am trying to show a non-animating modal before my main screen loads in my react-native app. 我试图在我的主屏幕加载到我的本机应用程序之前显示一个非动画模式。 Basically I want to check if a user is logged in, and if there is not a logged in user show a login screen on a modal view. 基本上,我想检查用户是否已登录,如果没有登录的用户,则在模式视图上显示登录屏幕。

The trouble is my main screen appears for a split second before my modal instead of the modal showing up first. 麻烦的是我的主屏幕出现在我的模态之前的瞬间,而不是模态先出现。 I am new to react/javascript in general and am not sure how to solve this. 我通常是新来的react / javascript,不确定如何解决这个问题。

I placed my login check in componentWillLoad thinking this would occur before the component is rendered on screen, but it doesnt seem to work. 我将登录检查放置在componentWillLoad中,以为这会在将组件呈现在屏幕上之前发生,但似乎不起作用。

export default class MyClass extends Component {

  state = {
    modalVisible: false,
  }

  componentWillMount() {
    // Check if a user is signed in
    firebase.auth().onAuthStateChanged(function(user) {
      if (user) {
        // User is signed in.
        console.log("Signed in");
        this.setState({modalVisible: false});
      } else {
        // No user is signed in.
        console.log("Not signed in");
        this.setState({modalVisible: true});
      }
    }.bind(this));
  }

  render() {

    return (
      <View>

        <NavigatorIOS
          initialRoute={{
            component: Things,
            title: 'MyScene',
          }}
          style={{flex: 1}}
        />

        <Modal
          animationType={"none"}
          transparent={false}
          visible={this.state.modalVisible}
          onRequestClose={() => {alert("Modal has been closed.")}}
          >
          <Login />
        </Modal>

      </View>

    );

  }
}

It is appearing for a split second because onAuthStateChanged call is asynchronous and react will not wait for that to complete and it moves on so your rendering happens before firebase returns. 由于onAuthStateChanged调用是asynchronous ,因此react不会等待它完成,而是继续进行,因此渲染发生在firebase返回之前。 So to solve this you have to wait till firebase returns. 因此,要解决此问题,必须等到Firebase返回。 To achieve this let's use state. 为了实现这一点,让我们使用状态。 Will create one state variable called isFirebaseReturned . 将创建一个名为isFirebaseReturned状态变量。 In the below code react will render NavigatorIos or Modal only if isFirebaseReturned = true 在下面的代码中,仅当isFirebaseReturned = true react才会呈现NavigatorIosModal

export default class MyClass extends Component {

  state = {
    modalVisible: false,
    isFirebaseReturned:false
  }

  componentWillMount() {
    // Check if a user is signed in
    firebase.auth().onAuthStateChanged(function(user) {
      if (user) {
        // User is signed in.
        console.log("Signed in");
        this.setState({modalVisible: false,isFirebaseReturned:true});
      } else {
        // No user is signed in.
        console.log("Not signed in");
        this.setState({modalVisible: true,isFirebaseReturned:true});
      }
    }.bind(this));
  }

  render() {

    return (
      <View>
      {this.state.isFirebaseReturned &&
        <NavigatorIOS
          initialRoute={{
            component: Things,
            title: 'MyScene',
          }}
          style={{flex: 1}}
        />

        <Modal
          animationType={"none"}
          transparent={false}
          visible={this.state.modalVisible}
          onRequestClose={() => {alert("Modal has been closed.")}}
          >
          <Login />
        </Modal>
      }
      </View>

    );

  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM