我是使用React Native的初学者。 我想为我的应用程序显示启动画面(1秒钟)并导航到不同的屏幕。 我已经关注并结合了一些教程,但是我收到了一个错误。
我的代码是这样的:
class SplashScreen extends React.Component {
static navigationOptions = {header: null,}
constructor(props){
super(props);
this.state = {
timePassed: false
};
}
render() {
let that = this;
setTimeout(function(){that.setState({timePassed: true})}, 1000);
const { navigate } = this.props.navigation;
if (!this.state.timePassed){
return (
<View style={styles.splashContainer}>
<Image source={require('./image/splash_screen.png')} style=
{styles.splash} />
</View>
);
}
else{
() => navigate('Login');
}
}
导航到新屏幕时出错。 谁能帮我?或者有更好的解决方案吗? 谢谢。
答案 0 :(得分:0)
试试这个
class SplashScene extends Component {
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function sleep(fn, ...args) {
await timeout(3000);
return fn(...args);
}
resetAndNavigate() {
const resetAction = NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'Login'})
]
})
this.props.navigation.dispatch(resetAction)
}
componentDidMount() {
sleep.then(
this.resetAndNavigate()
)
}
}