gpt4 book ai didi

javascript - react 导航 3 : Open modal with createBottomTabNavigator and createStackNavigator

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:53:31 25 4
gpt4 key购买 nike

我知道以前有人问过这个问题,但只针对旧版本的 react-navigation。从那时起,一些事情发生了变化。 createBottomTabNavigator 可以更快地创建底部导航器,函数 jumpToIndex() 不再存在。

我的问题是如何创建一个类似 Instagram 的底部选项卡,其中第一个、第二个、第四个和第五个导航按钮的作用与通常的选项卡导航器一样,中间按钮 (screen3) 打开模态 screen3Modal.

我已经在 react-navigation 3.x.x 中尝试过,使用 createBottomTabNavigatorcreateStackNavigator

import React, { Component, } from 'react';
import { createBottomTabNavigator, createStackNavigator, createAppContainer, } from 'react-navigation';
import { Screen1, Screen2, Screen3, Screen4, Screen5 } from './screens';

const TabNavigator = createBottomTabNavigator({
screen1: { screen: Screen1, },
screen2: { screen: Screen2, },
screen3: {
screen: () => null,
navigationOptions: () => ({
tabBarOnPress: () => this.props.navigation.navigate('screen3Modal')
})
},
screen4: { screen: Screen4, },
screen5: { screen: Screen5, },
});

const StackNavigator = createStackNavigator({
Home: { screen: TabNavigator },
screen3Modal: { screen: Screen3, },
},
{
initialRouteName: 'Home',
});

const StackNavigatorContainer = createAppContainer(StackNavigator);

export default class App extends Component {
render() {
return <StackNavigatorContainer />;
}
}

此代码创建选项卡导航和模态导航。模式可以从另一个屏幕打开,但它在选项卡导航器中不起作用。我收到错误消息 undefined is not an object (evaluating '_this.props.navigation')

最佳答案

我找到了一个相对简单的解决方案:用display:"none"

隐藏原来的导航栏
const TabNavigator = createBottomTabNavigator(
{
screen1: Screen1,
screen2: Screen2,
screen4: Screen4,
screen5: Screen5,
}, {
tabBarOptions: {
style: { display: "none", }
}
},
);

const StackNavigator = createStackNavigator(
{
Home: TabNavigator,
screen3: Screen3
}, {
mode: 'modal',
}
)

export default createAppContainer(StackNavigator);

并在每个屏幕上创建一个新的导航栏

<View style={{ flexDirection: "row", height: 50, justifyContent: "space-evenly", alignItems: "center", width: "100%" }}>
<TouchableOpacity onPress={() => this.props.navigation.navigate("screen1")}><Text>1</Text></TouchableOpacity>
<TouchableOpacity onPress={() => this.props.navigation.navigate("screen2")}><Text>2</Text></TouchableOpacity>
<TouchableOpacity onPress={() => this.props.navigation.navigate("screen3")}><Text>3</Text></TouchableOpacity>
<TouchableOpacity onPress={() => this.props.navigation.navigate("screen4")}><Text>4</Text></TouchableOpacity>
<TouchableOpacity onPress={() => this.props.navigation.navigate("screen5")}><Text>5</Text></TouchableOpacity>
</View>

关于javascript - react 导航 3 : Open modal with createBottomTabNavigator and createStackNavigator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54705813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com