2017-06-18 74 views
0

我想做一個應用程序,其中的飛濺顯示爲600毫秒,然後顯示主屏幕。我在componentDidmount中使用了setTimeOut方法,但是我得到的錯誤如here所示。下面還提供了必要的代碼。未定義不是函數:定時器 - 反應本機

import React, { Component } from 'react'; 
import {Image,Dimensions, Button, Text, StyleSheet, View, TouchableOpacity} from 'react-native'; 
var timePassed; 
class SplashScreen extends Component{ 
constructor(props){ 
    super(props); 
    this.state={ 
    timePassed: false 
    } 
} 

componentDidMount() { 
    this.setTimeout(() => { 
    this.setState({timePassed: true}) 
    },600); 
} 

render(){ 
    if(!this.state.timePassed){ 
    return(
<View style={styles.container}> 
<Image style={styles.logo} source={require('./Images/logo.jpg')} resizeMode="contain" /> 
<Text style={styles.deadlineFont}>Deadline</Text> 

</View> 
); 
} 
else{ 
    return (
    <View> 
    <MainScreen /> 
    </View> 
); 
} 
} 
    } 

回答

1

問題setTimeout被錯誤地稱爲與this,但不是在你的類中定義的方法。

解決方案:將this.setTimeout(() => {更改爲setTimeout(() => {

下面是您的代碼的一個工作示例:https://repl.it/Iqfk/1

+0

感謝您的工作! :) –

相關問題