clearInterval() 不会清除 setInterval 和影响导航

clearInterval() won't clear setInterval and Affect navigation

好吧,所以我有一个使用 setInterval 的启动画面,但问题是 setInterval 现在基本上影响了我在我的应用程序中的整个导航我尝试使用 clearInterval 但它赢了没用。

我尝试将 componentWillMount 更改为 componentDidMount,但仍然无效。

componentDidMount(){
  const splashInterval = setInterval(()=>{
      this.props.navigation.navigate('Main');
    },2000);
    this.setState({splashInterval: splashInterval});
}


componenWillUnmount(){
  const splashInterval = this.state.splashInterval;
  const clearSplashInterval = this.props.navigation.clearInterval(splashInterval);
  this.setState(clearSplashInterval);
}

为什么要从 props 获取 clearInterval?????

清除setInterval

这样做

componentDidMount(){
 this.inter = setInterval(async() => {
            this.props.navigation.navigate('Main');
        }, 2000);
}

componenWillUnmount(){
  clearInterval(this.inter);
}

clearInterval()函数清除之前setInterval()函数设置的间隔

你不需要清除间隔你可以简单地

  componentDidMount(){
    setTimeout(() => {
this.props.navigation.navigate("Main")
    }, 100);
  }

当你导航到另一个 class 时,你可以使用它,如果你想重置你的堆栈,就像你不希望堆栈中的初始屏幕一样

  componentDidMount(){
    setTimeout(() => {

const resetAction = StackActions.reset({
  index: 0,
  actions: [NavigationActions.navigate({ routeName: 'main' })],
});
this.props.navigation.dispatch(resetAction);
    }, 100);
  }