【发布时间】:2020-05-31 13:15:21
【问题描述】:
我正在 react native 项目中创建底部导航。它适用于以下编码。
App.js
import React from 'react';
import { createMaterialBottomTabNavigator } from '@react-navigation/material-bottom-tabs';
import { MaterialCommunityIcons } from 'react-native-vector-icons';
import { NavigationContainer } from '@react-navigation/native';
import Accounts from './src/components/Accounts';
// ...importing other screens here...
const Tab = createMaterialBottomTabNavigator();
function MyTabs() {
return (
<Tab.Navigator
initialRouteName="Feed"
activeColor="#e91e63"
labelStyle={{ fontSize: 12 }}
style={{ backgroundColor: 'tomato' }}>
<Tab.Screen name="Accounts" component={Accounts} />
...Other screens comes here...
</Tab.Navigator>
);
}
export default function App() {
return (
<NavigationContainer>
<MyTabs />
</NavigationContainer>
);
}
但我需要在标签中添加图标。所以我在Screen中添加了以下道具
<Tab.Screen
name="Accounts"
component={Accounts}
options={{
tabBarLabel: 'Home',
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons name="home" color={color} size={size} />
),
}}
/>
添加这些道具后,我收到以下错误
不变违规:元素类型无效:预期字符串(对于 内置组件)或类/函数(用于复合组件) 但未定义。您可能忘记从 它在其中定义的文件,或者您可能混淆了默认值和命名 进口
根据文档,我做的一切都是正确的。这些道具是从 React Navigation 文档中建议的。我的编码有什么问题?我需要标签中的图标
【问题讨论】:
标签: react-native react-navigation