【问题标题】:Where should propTypes for navigationOptions inside createBottomTabNavigator be defined?createBottomTabNavigator 中的 navigationOptions 的 propTypes 应该在哪里定义?
【发布时间】:2019-12-05 12:15:43
【问题描述】:

我刚刚应用了 airbnb、prettier、react/prettier 和所有的 linting。我仍然无法绕过这个错误[1],因为我不明白应该在哪里为“内部函数”声明 propTypes。

我没有给出这些参数,也没有定义它们。他们来自createBottomTabNavigator,我可以在文档中看到。那么,我应该如何对 tabBarIcon 需要哪些 props 以及哪些不需要有发言权?[2]

更新

[1] 错误是 linting 错误。代码执行得很好。

[2] 我们当然可以稍微摆弄一下以使其正常工作并避免错误,但我的目标是了解它是如何工作的以及为什么它会返回错误,因为 snippet 是官方示例。

export const HomeStackNavigator = createStackNavigator(
    ...
);

export const TabNavigator = createBottomTabNavigator(
  {
    Home: {
      screen: HomeStackNavigator,
    },
    Settings: {
      screen: SettingsStackNavigator,
    },
  },
  {
    initialRouteName: 'Home',
    defaultNavigationOptions: ({ navigation }) => ({
      tabBarIcon: ({ focused, horizontal, tintColor }) => {
        // Getting error here ^^^^^^^^^^^^^^^^^^^^^^^^
        // 'focused' is missing in props validationeslint(react/prop-types)
        const { routeName } = navigation.state;
        let IconComponent = Ionicons;
        let iconName;
        if (routeName === 'Home') {
          iconName = 'ios-home';
          IconComponent = HomeIconWithBadge;
        } else if (routeName === 'Settings') {
          iconName = 'ios-cog';
        }
        return (
          <IconComponent //
            name={iconName}
            color={tintColor}
            size={25}
          />
        );
      },
      headerStyle: {
        backgroundColor: '#f4511e',
      },
      headerTintColor: '#fff',
      headerTitleStyle: {
        fontWeight: 'bold',
      },
    }),
    tabBarOptions: {
      // activeTintColor: 'tomato',
      keyboardHidesTabBar: true,
    },
  }
);

export default class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      appState: AppState.currentState,
      isStoreLoading: false,
      store: createStore(rootReducer)
    };
  }

  ...

  componentDidMount(){...}

  ...

  render() {
    const { store, isStoreLoading } = this.state;
    if (isStoreLoading) {
      return <Text>Loading...</Text>;
    }
    return (
      <Provider store={store}>
        <AppContainer />
      </Provider>
    );
  }
}

【问题讨论】:

  • 您可以删除let IconComponent = Ionicons;IconComponent = HomeIconWithBadge;并使用&lt;Ionicons /&gt;
  • 这将如何帮助我知道应该在哪里定义 createBottomTabNavigator 中的 navigationOptions 的 propTypes?

标签: reactjs react-native eslint react-proptypes


【解决方案1】:

如果你真的想为这样的内部函数定义 prop-types,你需要把它移到导航器之外。

const MyTabBarIcon = ({ focused, horizontal, tintColor, navigation }) => {
    // [...]
}

MyTabBarIcon.propTypes = {
    focused: PropTypes.bool.isRequired,
    tintColor: PropTypes.string.isRequired,
    navigation: PropTypes.object.isRequired,
    horizontal: PropTypes.bool,
}

那么你的 TabNavigator 就变成了:

// [...]
defaultNavigationOptions: ({ navigation }) => ({
  tabBarIcon: props => <MyTabBarIcon {...props} navigation={navigation} />,
  // [...]
});
// [...]

【讨论】:

  • navigation 则不会被使用,也不会被定义。因为它在tabBarIcon 方法中使用。
  • 您可以将其添加为属性。我已经编辑了我的答案。
  • 对 linting 和解决方案不太满意,我觉得有点作弊。虽然还没有找到替代方案,但您的似乎足以“以适当的方式”做到这一点。
猜你喜欢
  • 2014-08-14
  • 1970-01-01
  • 2020-09-13
  • 2019-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-27
  • 2017-01-20
相关资源
最近更新 更多