【问题标题】:How to refresh a component which is already present in stack after navigating from another component in react native从反应原生的另一个组件导航后如何刷新堆栈中已经存在的组件
【发布时间】:2019-11-26 16:49:38
【问题描述】:

Ex- 我有两个组件 A 和 B。从组件 B 导航后我需要刷新组件 A。

componentDid 挂载不起作用,因为 A 已经挂载。

如何实现这一点。我正在使用反应导航从 B->A

导航

【问题讨论】:

    标签: react-native react-navigation


    【解决方案1】:

    您可以使用来自react-navigationNavigationEvents,也可以传递将在navigation.goBack() 上触发的回调。

    看看这个零食: Temporary Link

    【讨论】:

      【解决方案2】:

      你可以在 react native 中添加一个监听器。

      共有 4 个相同的侦听器。

      1. willFocus - 屏幕将聚焦
      2. didFocus - 屏幕聚焦(如果有过渡,则过渡完成)
      3. willBlur - 屏幕将失去焦点
      4. didBlur - 屏幕失焦(如果有过渡,则过渡完成)


      试试下面的代码

      componentDidMount(){
          const didFocusSubscription = this.props.navigation.addListener(
            'didFocus',
            payload => {
              console.warn('didFocus ', payload);
              # just write your code/call a method here which you want to execute when the app comes from a component
              this.handleRefresh()
            }
          );
      }
      

      完成后不要忘记删除它。

      componentWillMount(){
          didFocusSubscription.remove();
      }
      

      更多内容您可以阅读here。谢谢:)

      【讨论】:

      • 组件已安装(存在于堆栈中)。我需要去 A->B 和 B->A(刷新 A)。也不推荐使用 componentWillMount
      • 使用 componentDidMount() 代替...是的,如果您要导航回另一个组件,可以这样做。或者,如果您不想添加侦听器,您可以清除堆栈
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-08
      • 1970-01-01
      • 2018-03-19
      • 2019-01-22
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多