javascript - 如何使用 createBottomTabNavigator 为每个选项卡使用不同的图标?

标签 javascript reactjs react-native react-navigation

我正在为我的项目使用 React Native Navigation v2。我正在尝试使用 createBottomTabNavigator 设置两个不同的 IonIcons。

他们的 site举个这样的例子:

navigationOptions: ({ navigation }) => ({
  tabBarIcon: ({ focused, tintColor }) => {
    const { routeName } = navigation.state;
    let iconName;
    if (routeName === 'Home') {
      iconName = `ios-information-circle${focused ? '' : '-outline'}`;
    } else if (routeName === 'Settings') {
      iconName = `ios-options${focused ? '' : '-outline'}`;
    }

    // You can return any component that you like here! We usually use an
    // icon component from react-native-vector-icons
    return <Ionicons name={iconName} size={25} color={tintColor} />;
  },
}),

但对我来说,这似乎很乏味。我可以在组件本身中定义 IonIcon 吗? 如何使用 React Native Navigation v2 单独定义符号?

最佳答案

是的,你可以:

class HomeComponent extends Component {
    static navigationOptions = {
        tabBarIcon: ({ focused, tintColor }) => {
            const iconName = `ios-information-circle${focused ? '' : '-outline'}`;
            return <Ionicons name={iconName} size={25} color={tintColor} />;
        },
    };

    // ...
}

或者:

const tabs = createBottomTabNavigator({
    Home: {
        screen: HomeComponent,
        path: '/',
        navigationOptions: {
            tabBarIcon: ({ focused, tintColor }) => {
                const iconName = `ios-information-circle${focused ? '' : '-outline'}`;
                return <Ionicons name={iconName} size={25} color={tintColor} />;
            },
        },
    },
    // ...
});

关于javascript - 如何使用 createBottomTabNavigator 为每个选项卡使用不同的图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50854318/

相关文章:

javascript - 查找已定义的 sammy.js 路由列表

javascript - 限制 React Native TextInput 中的特殊字符

pinterest - 使用 React Native 的 Masonry/Pinterest 列

react-native - 将 babel polyfill 添加到 React Native 项目

javascript - 如何在 Nest 6 中注入(inject) HTTP 服务器?

javascript - Chrome 中的 Element.scrollIntoView() 方法动画速度太慢

javascript - 从循环中返回多个数字

javascript - 在 React 组件中从 json 填充列表项

reactjs - .htaccess 机器人代理

javascript - 我如何访问 React 中的方法以进行单元测试