【问题标题】:React navigation get title of previous screen反应导航获取上一个屏幕的标题
【发布时间】:2020-11-10 19:14:41
【问题描述】:

我将 React Navigation 5.x 用于我的 react 本机应用程序。我有一个堆栈导航器,其中包含仪表板和发票等多个屏幕。我的屏幕发票有一个自定义标题,所以我没有使用 React Navigation 标题选项。因此,我无法像 React Navigation 的标题那样在后退按钮中显示上一个屏幕的标题。

当然,我可以在自定义标题中放置一个自定义 GoBack 按钮,但我宁愿使用前一个屏幕的标题。

有没有办法获取用户来自的上一个屏幕/路线的标题?尝试了几种方法,例如使用 dangerouslyGetParent() ,但没有成功。

【问题讨论】:

    标签: javascript reactjs react-native react-navigation


    【解决方案1】:

    您可以将当前屏幕名称作为参数发送

    props.navigation.navigate("invoices", { previous_screen: "dashboard" } )
    

    并在下一个屏幕中读取该参数

    props.navigation.state.params.previous_screen
    

    【讨论】:

      【解决方案2】:

      您可以从传递给自定义标头的previous 属性中获取它。

      例子:

      function Header({ scene, previous }) {
        const { options } = scene.descriptor;
      
        let previousTitle;
      
        // The label for the back button shows the title of the previous screen
        // If a custom label is specified, we use it, otherwise use previous screen's title
        if (options.headerBackTitle !== undefined) {
          previousTitle = options.headerBackTitle;
        } else if (previous) {
          const o = previous.descriptor.options;
      
          previousTitle =
            typeof o.headerTitle !== 'function' && o.headerTitle !== undefined
              ? o.headerTitle
              : o.title !== undefined
              ? o.title
              : previous.route.routeName;
        }
      
        // Rest of the header content
      }
      
      

      【讨论】:

        猜你喜欢
        • 2018-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-08
        • 1970-01-01
        相关资源
        最近更新 更多