从一个类获取数据以使其成为stackNavigator选项卡的标题?

时间:2018-10-24 01:27:45

标签: reactjs react-native react-navigation navigationbar stack-navigator

import { createStackNavigator } from "react-navigation";
import React, { Component } from "react";

class Navigator extends React.Component {
  render() {
    const TitleNameParam = navigation.getParam('TitleNameParam');
    var TitleNameString = JSON.stringify(TitleNameParam);
    var TitleNameObject = JSON.parse(TitleNameString);
  }


}


const AppNavigator = createStackNavigator({
  Launching: {
    screen: LaunchingScreen,
    navigationOptions: {
      title: "Launching Soon",
      headerTitleAllowFontScaling: true,
      headerBackTitle: "Back"
    }
  },

  InfoScreen: {
    screen: InfoScreen,
    navigationOptions: {
      title: Navigator.TitleNameParam,
    }
  }
});



export default AppNavigator;

所以我要做的是将数据(从API)从一个类获取到堆栈导航器类,以便可以将标题替换为任何数据。我现在所拥有的是Navigator类(从TitleNameParam)获取数据,该数据使数据成为字符串,导航器在其中尝试在InfoScreen选项卡中获取数据,但没有显示任何内容。

1 个答案:

答案 0 :(得分:1)

在InfoScreen组件上使用navigationOptions:

class InfoScreen extends React.Component {
  // Set the navigation options for `react-navigation`
  static navigationOptions = ({navigation}) => {
    return {
      headerTitle: navigation.getParam('TitleNameParam');
    }
  }
…
}

导航到此路线时,请使用:

this.props.navigation.navigate('InfoScreen', {TitleNameParam: yourComputedTitleName})

请让我知道这是否正确。