【问题标题】:React native splash screen and navigate to different screen反应原生闪屏并导航到不同的屏幕
【发布时间】:2017-09-11 03:49:50
【问题描述】:

我是使用 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'); 
    }

}

导航到新屏幕时出错。 谁能帮我?还是有更好的解决方案? 谢谢。

【问题讨论】:

  • 错误是什么?
  • 该错误是在屏幕即将导航时(1 秒后)出现还是在应用刚开始时出现?
  • 屏幕即将导航时出现错误@rabbit87我忘记了错误信息,稍后会在这里发布
  • @ParasWatts 错误消息是:SplashScreen.render():必须返回有效的反应元素(或 null)。您可能返回了未定义、数组或其他一些有效对象。
  • 可能错误在你的 else 部分

标签: reactjs react-native navigation stack-navigator


【解决方案1】:

试试这个

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()
    )
  }
}

【讨论】:

  • 您好,谢谢您的回答。你能告诉我如何使用这个功能吗?我的意思是你能解释一下如何用渲染函数来实现吗?谢谢
  • 你是否导入了登录组件?给我完整的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 2019-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多