【发布时间】:2021-03-29 12:17:54
【问题描述】:
我刚刚开始学习 React Native。我正在尝试在我的第一个应用程序中插入底部菜单选项卡。
我在 Tabs.js 上使用此代码(这只是导出部分):
export default function Tabs() {
return (
<NavigationContainer>
<MyTabs />
</NavigationContainer>
);
}
不幸的是,我不知道如何将它调用到我的主 App 文件中(这只是我的尝试之一):
import * as React from 'react';
import { Text, View } from 'react-native';
import Tabs from './Tabs.js';
Tabs();
我已阅读有关导出默认功能的信息,但我不明白如何在我的主应用程序文件中使用它。我确定这是语法问题。
另外,我计划为所有选项卡添加背景颜色。有什么建议吗?
谢谢。
更新:
这是我的标签文件。
import * as React from 'react';
import { Text, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { MaterialCommunityIcons } from '@expo/vector-icons';
const Tabs = () => {
function Feed() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text></Text>
</View>
);
}
function Profile() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text></Text>
</View>
);
}
function MainPage() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text></Text>
</View>
);
}
const Tab = createBottomTabNavigator();
function MyTabs() {
return (
<Tab.Navigator
initialRouteName="Feed"
tabBarOptions={{
activeTintColor: '#e91e63',
}}
>
<Tab.Screen
name="Feed"
component={Feed}
options={{
tabBarLabel: '',
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons name="home" color={color} size={size} />
),
}}
/>
<Tab.Screen
name="MainPage"
component={MainPage}
options={{
tabBarLabel: '',
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons name="pill" color={color} size={size} />
),
}}
/>
<Tab.Screen
name="Profile"
component={Profile}
options={{
tabBarLabel: '',
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons name="format-list-text" color={color} size={size} />
),
}}
/>
</Tab.Navigator>
);
}
return (
<NavigationContainer>
<MyTabs />
</NavigationContainer>
);
}
export default Tabs
这是我的主要应用文件。
import * as React from 'react';
import { Text, View } from 'react-native';
import Tabs from './Tabs.js';
const App=()=>{
return <Tabs />
}
【问题讨论】:
-
你应该在
return的App组件的渲染函数中写上<Tabs />。见reactjs.org/docs/… -
感谢您提供的信息。我已按照建议更新了我的代码。我仍然缺少一些东西。
-
错误:元素类型无效:应为字符串(用于内置组件)或类/函数(用于复合组件),但得到:未定义。您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。请记住,我对此很陌生,所以我可能会遗漏一些非常明显的东西。对此感到抱歉。
-
您还需要导出
App组件才能在某处导入并使用它,所以,您需要const App = () => { return <Tabs /> }; export default App; -
谢谢!有用。不知道如何支持这个答案,但这很有帮助。
标签: javascript reactjs react-native