让我们假设堆栈导航器中有两个屏幕:
第一屏->第二屏
如何通过调用this.props.navigation.goBack()返回到前一个屏幕来刷新/重新加载它?
我正在使用以下react-native和react-navigation版本:
"react-native": "0.63.2",
"@react-navigation/native": "^5.7.3",
"@react-navigation/stack": "^5.9.0",
我尝试了以下方法,
componentDidMount() {
this.props.fetchData();
this.willFocusSubscription = this.props.navigation.addListener(
'willFocus',
() => {
this.props.fetchData();
}
);
}
componentWillUnmount() {
this.willFocusSubscription.remove();
}
但是没有得到预期的结果。
答案 0 :(得分:1)
尝试一下,它将起作用
componentDidMount() {
this.onFocusCall = this.props.navigation.addListener('willFocus', () => {
this.apiCall();
});
}
componentWillUnmount() {
this.onFocusCall.remove();
}
答案 1 :(得分:1)
由于您使用的是5倍反应导航,因此需要将侦听器更新为关注
np.frombuffer(data, np.uint8)
docs:https://reactnavigation.org/docs/function-after-focusing-screen/
答案 2 :(得分:1)
尝试一下,一切正常
useEffect(() => {
const unsubscribe = navigation.addListener("focus", async () => {
const response = await apiCall();
});
return unsubscribe;
}, [navigation]);