在componentDidMount()之后,在react原生中,当你导航到其他屏幕并返回到你的组件(屏幕)时,它不会再次调用。因此react原生导航建议通过以下JS代码来实现:
componentDidMount() {
this._Reload = this.props.navigation.addListener('focus', () => {
//do something to reset component
});
}
componentWillUnmount() {
this._Reload();
}但是现在我得到了这个错误:undefined is not an object (evaluating 'this.props.navigation.addListener'),因为我在选项卡组件(嵌套)中使用了我的组件。如何将焦点添加到要刷新的组件?我想我必须通过道具钻探来完成。
发布于 2020-09-28 15:40:09
我建议通过上下文传递父屏幕导航对象(以便拥有焦点侦听器),然后在选项卡组件中将侦听器设置为父导航。
有关如何将数据传递到选项卡组件的参考信息,请查看以下内容:Passing params to tab navigator React Navigation 5
或者,对于功能组件,您可以使用这个钩子:来自@react-navigation/native的useFocusEffect
https://stackoverflow.com/questions/64097809
复制相似问题