setTimeout in React Native

前端 未结 11 2096
没有蜡笔的小新
没有蜡笔的小新 2020-12-14 05:43

I\'m trying to load a splash screen for an iOS app built in React Native. I\'m trying to accomplish this through class states and then a setTimeout function as follows:

相关标签:
11条回答
  • 2020-12-14 06:02

    Change this code:

    setTimeout(function(){this.setState({timePassed: true})}, 1000);
    

    to the following:

    setTimeout(()=>{this.setState({timePassed: true})}, 1000); 
    
    0 讨论(0)
  • 2020-12-14 06:04

    Same as above, might help some people.

    setTimeout(() => {
      if (pushToken!=null && deviceId!=null) {
        console.log("pushToken & OS ");
        this.setState({ pushToken: pushToken});
        this.setState({ deviceId: deviceId });
        console.log("pushToken & OS "+pushToken+"\n"+deviceId);
      }
    }, 1000);
    
    0 讨论(0)
  • 2020-12-14 06:06

    On ReactNative .53, the following works for me:

     this.timeoutCheck = setTimeout(() => {
       this.setTimePassed();
       }, 400);
    

    'setTimeout' is the ReactNative library function.
    'this.timeoutCheck' is my variable to hold the time out object.
    'this.setTimePassed' is my function to invoke at the time out.

    0 讨论(0)
  • 2020-12-14 06:09

    Write a new function for settimeout. Pls try this.

    class CowtanApp extends Component {
      constructor(props){
      super(props);
      this.state = {
      timePassed: false
      };
    }
    
    componentDidMount() {
      this.setTimeout( () => {
         this.setTimePassed();
      },1000);
    }
    
    setTimePassed() {
       this.setState({timePassed: true});
    }
    
    
    render() {
    
    if (!this.state.timePassed){
      return <LoadingPage/>;
    }else{
      return (
        <NavigatorIOS
          style = {styles.container}
          initialRoute = {{
            component: LoginPage,
            title: 'Sign In',
          }}/>
      );
    }
    }
    }
    
    0 讨论(0)
  • 2020-12-14 06:10

    There looks to be an issue when the time of the phone/emulator is different to the one of the server (where react-native packager is running). In my case there was a 1 minute difference between the time of the phone and the computer. After synchronizing them (didn't do anything fancy, the phone was set on manual time, and I just set it to use the network(sim) provided time), everything worked fine. This github issue helped me find the problem.

    0 讨论(0)
提交回复
热议问题