gpt4 book ai didi

react-native - 如何同时实现DrawerNavigator和StackNavigator

转载 作者:行者123 更新时间:2023-12-03 12:46:52 26 4
gpt4 key购买 nike

我正在使用react-native-navigation开发一个应用程序,并且我想在项目中具有StackNavigator和DrawerNavigator。因此,我已经在app.js中实现了它们,但应用程序崩溃并给出此错误“开发服务器返回了响应错误,代码为500”。我已经分别实现了它们,并且可以正常工作,但是我无法一起实现。 ?

这是我的app.js代码

import React, {
Component
} from 'react';

import {
StyleSheet,
Text,
View
} from 'react-native';

import {
createStackNavigator,
DrawerNavigator,
DrawerItems
} from "react-navigation";

import {
Container,
Header,
Content,
Thumbnail,
Button,
Body
} from 'native-base';

import Profile from './components/Profile.js';
import Main from './components/Main.js';
import Login from './components/Login.js';
import Product from './components/Product.js';

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

const styles = StyleSheet.create({
// styles here
});

export const Drawer = DrawerNavigator({
Main: {
screen: Main
},
Profile: {
screen: Profile
},
}, {
initialRouteName: 'Main',
contentComponent: CustomDrawer,
drawerPosition: 'Left',
drawerOpenRoute: 'DrawerOpen',
drawerCloseRoute: 'DrawerClose',
drawerToggleRoute: 'DrawerToggle',
});

export const CustomDrawer = (props) => (
<Container>
<Header style = {
styles.headerStyle
}>
<Body style = {
styles.bodyStyle
}>
<Thumbnail style = {
{
height: 100,
width: 100
}
}
source = {
require('../image/logo.png')
}/>
</Body>
</Header>
<Content>
<DrawerItems { ...props} />
</Content >
</Container>
)

export const Navapp = createStackNavigator({
Login: {
screen: Login
},
Drawer: {
screen: Drawer
},
Product: {
screen: Product
},
});

最佳答案

我为我的应用设置了非常相似的设置,这就是我处理它的方式。首先,我创建了一个堆栈导航器,其中包含要登录用户查看的路由,然后将该导航器放置在抽屉式导航器中(如果需要,可以在其中放置多个)。最后,我创建了我的顶级导航器,其初始路径指向登录页面。登录后,我将用户导航到第二条路线,该路线指向我的抽屉导航器。

实际上,它看起来像这样:

// Main Screens for Drawer Navigator
export const MainStack = StackNavigator({
Dashboard: {
screen: Dashboard,
navigationOptions: {
title: 'Dashboard',
gesturesEnabled: false,
headerLeft: null
}
},

Notifications: {
screen: Notifications,
navigationOptions: {
title: 'Notifications'
}
}
}, { headerMode: 'screen' } );

// Drawer Navigator
export const Drawer = DrawerNavigator({
MainStack: {
screen: MainStack
}
});


// Main App Navigation
export const AppStack = StackNavigator({
Login: {
screen: Login,
navigationOptions: {
header: null,
gesturesEnabled: false
}
},

Drawer: {
screen: Drawer,
navigationOptions: {
header: null,
gesturesEnabled: false
}
}
}, { headerMode: 'none' } );

// In Your App.js
<AppStack />

请注意,在最后一个堆栈导航器中,抽屉屏幕的标题设置为null。这是因为使用嵌套堆栈导航器有时会遇到标题重复的问题。这将隐藏顶级导航器的标题,并让您显示/自定义嵌套导航器的标题。

关于react-native - 如何同时实现DrawerNavigator和StackNavigator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50290050/

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