如何将 createBottomTabNavigator 添加到与 createStackNavigator 相同的屏幕
How to add createBottomTabNavigator to same screen with createStackNavigator
在我的主屏幕上(我使用 App.js 作为我的家),我有 createStackNavigator 和 createAppContainer 来使用 React-Navigation 映射所有屏幕。它起作用了,我可以从任何屏幕明确导航到这些屏幕中的任何一个。现在我想用 "createBottomTabNavigator".
添加一个底部标签导航栏到主屏幕
这是我的代码的简化版本:
import { createStackNavigator, createAppContainer, createBottomTabNavigator } from 'react-navigation';
import Screen1 from './Screens/Screen1';
import Screen2 from './Screens/Screen2';
import Screen3 from './Screens/Screen3';
class HomeScreen extends React.Component {
render() {
return (
<View><Text>Welcome</Text></View>
);
}
}
const AppNavigator = createStackNavigator({
Home: { screen: HomeScreen },
Screen1: { screen: Screen1 },
Screen2: { screen: Screen2 },
Screen3: { screen: Screen3 },
});
export default createAppContainer(AppNavigator);
我想在底部添加一个导航栏是这样的:
const TabNavigator = createBottomTabNavigator({
Home: { screen: HomeScreen },
Screen3: { screen: Screen3 },
});
export default createAppContainer(TabNavigator);
不允许在同一页面中出现两个 "export default"。如何在保持 "createStackNavigator" 映射我所有屏幕的同时添加 "createBottomTabNavigator"?
您可以使用如下代码:
import { createStackNavigator, createAppContainer, createBottomTabNavigator } from 'react-navigation';
import Screen1 from './Screens/Screen1';
import Screen2 from './Screens/Screen2';
import Screen3 from './Screens/Screen3';
class HomeScreen extends React.Component {
render() {
return (
<View><Text>Welcome</Text></View>
);
}
}
const AppNavigator = createStackNavigator({
Home: { screen: TabNavigator },
Screen1: { screen: Screen1 },
Screen2: { screen: Screen2 },
Screen3: { screen: Screen3 },
});
const TabNavigator = createBottomTabNavigator({
Home: { screen: HomeScreen },
Screen3: { screen: Screen3 },
});
export default createAppContainer(AppNavigator);
像这样。
import { createStackNavigator, createAppContainer, createBottomTabNavigator } from 'react-navigation';
import Screen1 from './Screens/Screen1';
import Screen2 from './Screens/Screen2';
import Screen3 from './Screens/Screen3';
import Screen4 from './Screens/Screen4';
const Home = createStackNavigator({
Screen1, Screen2
});
const List = createStackNavigator({
Screen3, Screen4
});
const TabNav = createBottomTabNavigator({
Home, List
});
export default createAppContainer(TabNav);
这里你的主页可以是 Screen1
和 Screen2
可以是 Screen1 的一个项目,对于 Screen3 and Screen4
在我的主屏幕上(我使用 App.js 作为我的家),我有 createStackNavigator 和 createAppContainer 来使用 React-Navigation 映射所有屏幕。它起作用了,我可以从任何屏幕明确导航到这些屏幕中的任何一个。现在我想用 "createBottomTabNavigator".
添加一个底部标签导航栏到主屏幕这是我的代码的简化版本:
import { createStackNavigator, createAppContainer, createBottomTabNavigator } from 'react-navigation';
import Screen1 from './Screens/Screen1';
import Screen2 from './Screens/Screen2';
import Screen3 from './Screens/Screen3';
class HomeScreen extends React.Component {
render() {
return (
<View><Text>Welcome</Text></View>
);
}
}
const AppNavigator = createStackNavigator({
Home: { screen: HomeScreen },
Screen1: { screen: Screen1 },
Screen2: { screen: Screen2 },
Screen3: { screen: Screen3 },
});
export default createAppContainer(AppNavigator);
我想在底部添加一个导航栏是这样的:
const TabNavigator = createBottomTabNavigator({
Home: { screen: HomeScreen },
Screen3: { screen: Screen3 },
});
export default createAppContainer(TabNavigator);
不允许在同一页面中出现两个 "export default"。如何在保持 "createStackNavigator" 映射我所有屏幕的同时添加 "createBottomTabNavigator"?
您可以使用如下代码:
import { createStackNavigator, createAppContainer, createBottomTabNavigator } from 'react-navigation';
import Screen1 from './Screens/Screen1';
import Screen2 from './Screens/Screen2';
import Screen3 from './Screens/Screen3';
class HomeScreen extends React.Component {
render() {
return (
<View><Text>Welcome</Text></View>
);
}
}
const AppNavigator = createStackNavigator({
Home: { screen: TabNavigator },
Screen1: { screen: Screen1 },
Screen2: { screen: Screen2 },
Screen3: { screen: Screen3 },
});
const TabNavigator = createBottomTabNavigator({
Home: { screen: HomeScreen },
Screen3: { screen: Screen3 },
});
export default createAppContainer(AppNavigator);
像这样。
import { createStackNavigator, createAppContainer, createBottomTabNavigator } from 'react-navigation';
import Screen1 from './Screens/Screen1';
import Screen2 from './Screens/Screen2';
import Screen3 from './Screens/Screen3';
import Screen4 from './Screens/Screen4';
const Home = createStackNavigator({
Screen1, Screen2
});
const List = createStackNavigator({
Screen3, Screen4
});
const TabNav = createBottomTabNavigator({
Home, List
});
export default createAppContainer(TabNav);
这里你的主页可以是 Screen1
和 Screen2
可以是 Screen1 的一个项目,对于 Screen3 and Screen4