RN 从特定屏幕返回后刷新父屏幕

问题描述 投票:0回答:2

我有主屏幕 A。从 A 可以导航 B、C、D 屏幕。 在大多数情况下,我需要在焦点事件上刷新屏幕 A,只有 A 在从 D 导航回来时不应刷新。

如何使用 hooks 在 React Native 中实现这个场景?

react-native react-hooks navigation wix-react-native-navigation react-native-navigation-v2
2个回答
0
投票

我认为你可以使用像这样的反应导航生命周期事件

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


0
投票

下面的东西对我有用

在家长

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)
           ......
    }
    
}
© www.soinside.com 2019 - 2024. All rights reserved.