在 goBack 上重新加载屏幕反应原生 [英] Reload Screen on goBack react native
本文介绍了在 goBack 上重新加载屏幕反应原生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设一个堆栈导航器中有两个屏幕:
Lets assume there are two screens in a stack navigator:
First_Screen ->第二_屏幕
First_Screen -> Second_Screen
如何在调用 this.props.navigation.goBack() 返回之前刷新/重新加载前一个屏幕?
How can we refresh/reload previous screen when returning to it by calling this.props.navigation.goBack()?
我正在使用以下 react-native 和 react-navigation 版本:
I am using the below react-native and react-navigation versions:
"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();
}
但是没有得到预期的结果.
But did not get the expected result.
推荐答案
试试这个,效果不错
useEffect(() => {
const unsubscribe = navigation.addListener("focus", async () => {
const response = await apiCall();
});
return unsubscribe;
}, [navigation]);
这篇关于在 goBack 上重新加载屏幕反应原生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文