我试图在两个屏幕之间导航,但它不起作用 这是我的代码 当我运行代码时,它会出错
import React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
import {MainPage, ListPage, DetailPage} from './pages';
const Stack = createStackNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Screen name="Main" component={MainPage} />
<Stack.Screen name="List" component={ListPage} />
<Stack.Screen name="Detail" component={DetailPage} />
</NavigationContainer>
);
}
export default App;
答案 0 :(得分:0)
将您的代码更新为以下内容。
import React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
import {MainPage, ListPage, DetailPage} from './pages';
const Stack = createStackNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Main" component={MainPage} />
<Stack.Screen name="List" component={ListPage} />
<Stack.Screen name="Detail" component={DetailPage} />
</Stack.Navigator>
</NavigationContainer>
);
}
export default App;
您可以找到有关如何使用 Stack Navigator here 的更多详细信息。
答案 1 :(得分:0)
在 <Stack.Navigator>
标签后使用 <NavigationContainer>
标签
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Main" component={MainPage} />
<Stack.Screen name="List" component={ListPage} />
<Stack.Screen name="Detail" component={DetailPage} />
</Stack.Navigator>
</NavigationContainer>