【问题标题】:How to get previous scene name in react native router flux如何在反应本机路由器通量中获取先前的场景名称
【发布时间】:2018-07-16 07:13:44
【问题描述】:

我需要使用 react-native-router-flux 在 componentWillMount 中获取先前的场景键,以检查我从哪个屏幕返回并根据条件进行一些操作。我看了很多,但没有找到答案。有没有办法做到这一点?

【问题讨论】:

    标签: react-native-router-flux


    【解决方案1】:

    你可以通过 Actions.prevScene, 这将返回堆栈中上一个场景的键。

    【讨论】:

    • 返回当前场景名称
    【解决方案2】:

    很难说,但更容易的是在导航时传递变量,就像那里一样:

    Actions.home({from: 'about'})
    

    现在,Home 属性包含 from 变量,你可以处理这个。

    【讨论】:

      【解决方案3】:

      @sftgf 的解决方案应该可以工作,但在 react-native-router-flux 4.0.0-beta.28 上,它错误地为我返回了当前场景。

      这个Gist 工作并具有将其他场景放入堆栈的额外好处:

      import {Actions} from react-native-reouter-flux
      
      export function getPreviousScene (jumpCount = 1) { // if nothing is passed, it defaults to 1
        const index = Actions.prevState.routes.length - (jumpCount + 1) // because zero-based index
      
        if (index >= 0) {
          return Actions.prevState.routes[index].routeName
        }
      
        return 'home'
      }
      

      【讨论】:

        猜你喜欢
        • 2018-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多