【发布时间】: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;并使用<Ionicons /> -
这将如何帮助我知道应该在哪里定义 createBottomTabNavigator 中的 navigationOptions 的 propTypes?
标签: reactjs react-native eslint react-proptypes