【问题标题】:React Navigator pass NavigationOptions propReact Navigator 通过 NavigationOptions 道具
【发布时间】:2019-05-08 04:06:39
【问题描述】:

我已经使用 React Navigator 3.0 设置了 Navigation 为了成功地从父组件传递道具,我这样调用我的 TabNavigator:

return (
        <View style={styles.container}>
          <TabNavigator screenProps={{deleteToken: this.deleteJWT }} />
        </View>
      );

然后我的 TabNavigator 组件会呈现几个屏幕。我之前的方法是使用文档中的默认代码(HomeScreen 如下所示),但现在由于我要传递道具,我需要使用 ProfileScreen (箭头函数)如下所示的方法。

我的问题是我不知道在使用箭头功能时将 ProfileScreen 的 navigationOptions 放在哪里。

const TabNavigator = createBottomTabNavigator(
  {
  Profile: (props) => {
    return <ProfileScreen {...props.screenProps} />;
  },
  Home: { screen: HomeScreen,
          navigationOptions: {
            //tabBarLabel: 'Inicio',
            tabBarIcon: ({ tintColor, focused }) => (
          <Ionicons
            name={focused ? 'ios-home' : 'ios-home'}
            size={26}
            style={{ color: tintColor }}
          />
        ),
          //  tabBarIcon: () => {
          //                <Image
          //                style={{ width: 50, height: 50 }}
          //                source={{ uri: 'https://facebook.github.io/react/img/logo_og.png' }}
          //                />
        //},
        }
      },

更新:显而易见的地方是将 NavigationOptions 对象放在返回的屏幕中,但它被忽略了:

export default class ProfileScreen extends React.Component {
  static navigationOptions = {

        tabBarLabel: 'Perfil',
        tabBarIcon: ({ tintColor, focused }) => (
      <Ionicons
        name={focused ? 'ios-person' : 'ios-person'} //TODO change to focused icon
        size={26}
        style={{ color: tintColor }}
      />
    )
  }

【问题讨论】:

    标签: react-native react-navigation


    【解决方案1】:

    这是正确的语法:

    const TabNavigator = createBottomTabNavigator(
      {
      Profile: {
        screen: props => <ProfileScreen {...props.screenProps} />,
        navigationOptions: {
            //tabBarLabel: 'Perfil',
            tabBarIcon: ({ tintColor, focused }) => (
          <Ionicons
            name={focused ? 'ios-person' : 'ios-person'} //TODO change to focused icon
            size={26}
            style={{ color: tintColor }}
          />
        ),
    

    【讨论】:

    • 如果你投了反对票,你应该解释为什么?此语法是正确的,并且在生产中运行良好,并且回答了原始问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多