【发布时间】:2020-06-28 18:55:32
【问题描述】:
每次我到达特定屏幕(在我的例子中是主页)时,我都会尝试重置导航堆栈。
这是一段代码:
componentDidMount(){
const { navigation } = this.props
this.focusListener = navigation.addListener('focus', () => {
this._getData()
console.log('coucou')
navigation.dispatch(
CommonActions.reset({
index: 0,
routes: [
{ name: 'Home' },
],
})
);
});
this._updateNavigationParams()
}
componentWillUnmount() {
// Remove the event listener before removing the screen from the stack
this.focusListener.remove();
}
如果我删除以下部分,我的代码可以正常运行:
navigation.dispatch(
CommonActions.reset({
index: 0,
routes: [
{ name: 'Home' },
],
})
);
我需要一个监听器,因为当我回到 HomeScreen 时我必须刷新数据,而且每次回到这里时我都会使用它来重置导航堆栈。
我得到的错误是:
TypeError: this.focusListener.remove 不是函数。 (在 'this.focusListener.remove()', 'this.focusListener.remove' 是 未定义)。
【问题讨论】:
标签: react-native react-navigation expo focuslistener