React Navigation clearInterval不起作用

问题描述 投票:0回答:2

反应导航堆栈clearInterval不起作用

App.js示例

const AppNavigator = createStackNavigator({
  Home: {
    screen: Home,
  },
  Contact: {
    screen: Contact,
  }
});

Home.js示例

componentDidMount() {
   this.interval = setInterval(this.load, 10000);
}
componentWillUnMount(){
   clearInterval(this.interval);
}

当我从家中切换到联系人时,间隔仍然有效。请帮助我。

react-native react-navigation expo react-navigation-stack
2个回答
0
投票

实际上,我认为导航到另一个屏幕不会卸载该组件。

因此,我建议导航到“联系人屏幕”时执行以下操作:

import { StackActions, NavigationActions } from 'react-navigation';
    
    const resetAction = StackActions.reset({
      index: 0,
      actions: [NavigationActions.navigate({ routeName: 'Contact' })],
    });
    this.props.navigation.dispatch(resetAction);

0
投票
enter code here 

Blockquotestrongstrong

© www.soinside.com 2019 - 2024. All rights reserved.