我有主屏幕 A。从 A 可以导航 B、C、D 屏幕。 在大多数情况下,我需要在焦点事件上刷新屏幕 A,只有 A 在从 D 导航回来时不应刷新。
如何使用 hooks 在 React Native 中实现这个场景?
我认为你可以使用像这样的反应导航生命周期事件
function Profile({ navigation }) {
React.useEffect(() => {
const unsubscribe = navigation.addListener('focus', () => {
// Screen was focused
// Do something
});
return unsubscribe;
}, [navigation]);
return <ProfileContent />;
}
这里是文档:https://reactnavigation.org/docs/navigation-lifecycle#react-navigation-lifecycle-events
下面的东西对我有用
在家长
const OnRefreshNeededFromChild = (isSuccess: boolean) => {
console.log("On Returned called")
if (isSuccess) {
RefreshData()
}
}
const FromParentToChild = () => {
navigation.navigate("childpage", { OnReturn: OnRefreshNeededFromChild })
}
儿童时期
export childFunction(props: any){
const OnRetun = props.route.params.OnReturn
const OnButtonClick = async () => {
.......
//Calling the parent callback here
OnRetun && OnRetun(true)
......
}
}