Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setTimeout in React Native

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:

class CowtanApp extends Component {   constructor(props){     super(props);     this.state = {       timePassed: false     };   }    render() {     setTimeout(function(){this.setState({timePassed: true})}, 1000);     if (!this.state.timePassed){       return <LoadingPage/>;     }else{       return (         <NavigatorIOS           style = {styles.container}           initialRoute = {{             component: LoginPage,             title: 'Sign In',           }}/>       );     }   } } 

The loading page works for a second, and then I guess when setTimeout tries to change the state to true, my program crashes: 'undefined is not an object (evaluating this.setState)'. I've been going at this for a couple of hours, any ideas on how to fix it?

like image 467
Phil Avatar asked Dec 29 '15 04:12

Phil


People also ask

How setTimeout works in react native?

The yourFunction will execute only after 3000 milliseconds, that is 3 seconds. setTimeout(() => { yourFunction(); }, 3000); In the following react native setTimeout example, the method is used inside componentDidMount lifecycle. When the time period of 5 seconds finishes the alert will appear on the screen.

How do I delay in react native?

Define a function that takes the number of milliseconds as parameter. Use the setTimeout method to resolve a Promise after the provided number of milliseconds.

How do you stop setTimeout in react native?

Clearing setTimeout To clear or cancel a timer, you call the clearTimeout(); method, passing in the timer object that you created into clearTimeout().

What is difference between setInterval and setTimeout?

setTimeout allows us to run a function once after the interval of time. setInterval allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval.


2 Answers

Classic javascript mistake.

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

When setTimeout runs this.setState, this is no longer CowtanApp, but window. If you define the function with the => notation, es6 will auto-bind this.

setTimeout(() => {this.setState({timePassed: true})}, 1000) 

Alternatively, you could use a let that = this; at the top of your render, then switch your references to use the local variable.

render() {   let that = this;   setTimeout(function(){that.setState({timePassed: true})}, 1000); 

If not working, use bind.

setTimeout(   function() {       this.setState({timePassed: true});   }   .bind(this),   1000 ); 
like image 170
oliversisson Avatar answered Sep 16 '22 15:09

oliversisson


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',       }}/>   ); } } } 
like image 44
Phyo Avatar answered Sep 18 '22 15:09

Phyo