gpt4 book ai didi

javascript - react native : Toggle drawer & navigation bar visibility in routes

转载 作者:行者123 更新时间:2023-11-29 10:09:43 27 4
gpt4 key购买 nike

我有一个应用程序需要 DrawerNavigator。为实现这一点,我在 index.ios.js 中有一个 Drawer 组件作为父组件,它封装了其他所有内容。

现在我要解决的用例是,我必须在某些路由中禁用 Drawer 或隐藏 NavigationBar。为此,我正在更新父组件的状态并在每次导航时重新呈现。然而,这似乎并没有帮助。

index.io.js :

class MyApp extends Component{

state = {drawerEnabled:true, navigationBarEnabled: true};

constructor(){
super();
this.navigate = this.navigate.bind(this);
this.navigateForDrawer = this.navigateForDrawer.bind(this);
this.getNavigator = this.getNavigator.bind(this);
this.renderScene = this.renderScene.bind(this);
}

navigateForDrawer(route) {
this.refs.navigator.resetTo(route);
this.refs.drawer.close();
}

getNavigationBar(){
return (
this.state.navigationBarEnabled ?
<Navigator.NavigationBar
style={styles.navBar}
transitionStyles={Navigator.NavigationBar.TransitionStylesIOS}
routeMapper={NavigationBarRouteMapper}
/> : null);
}

getNavigator(){
return (
<Navigator
ref="navigator"
style={styles.navigator}
initialRoute={{id:'Login'}}
renderScene={this.renderScene}
navigationBar={this.getNavigationBar()}
/>
);
}

navigate(route, method){
if(route)
switch (route.id) {
case 'Login':
this.state = {drawerEnabled: false, navigationBarEnabled: false};
break;

case 'NewBooking':
this.state = {drawerEnabled: true, navigationBarEnabled: true};
break;

case 'MyBookings':
this.state = {drawerEnabled: true, navigationBarEnabled: true};
break;

case 'AboutUs':
this.state = {drawerEnabled: true, navigationBarEnabled: true};
break;

case 'ConfirmBooking':
this.state = {drawerEnabled: false, navigationBarEnabled: true};
break;

case 'BookingComplete':
this.state = {drawerEnabled: false, navigationBarEnabled: true};
break;
}

this.forceUpdate();
this.refs.navigator[method](route);
}

renderScene(route, navigator){
navigator.navigate = this.navigate;
switch (route.id) {
case 'Login':
return <Login navigate={this.navigate}/>;

case 'NewBooking':
route.title = 'New Booking';
return <NewBooking navigate={this.navigate}/>;

case 'MyBookings':
route.title = 'My Bookings';
return <MyBookings navigate={this.navigate}/>;

case 'AboutUs':
route.title = 'About Us';
return <AboutUs navigate={this.navigate}/>;

case 'ConfirmBooking':
route.title = 'Booking Summary';
return <ConfirmBooking navigate={this.navigate} booking={route.booking}/>;

case 'BookingComplete':
route.title = 'Booking Complete';
return <BookingComplete navigate={this.navigate} booking={route.booking}/>;

default:
}
}

render() {
return (
<Drawer
ref="drawer"
disabled={!this.state.drawerEnabled}
content={<ControlPanel navigate={this.navigateForDrawer}/>}
tapToClose={true}
openDrawerOffset={0.2} // 20% gap on the right side of drawer
panCloseMask={0.2}
closedDrawerOffset={-3}
styles={{
main: {paddingLeft: 3}
}}
tweenHandler={(ratio) => ({
main: { opacity:(2-ratio)/2 }
})}
>
{this.getNavigator()}
</Drawer>
)
}

}

导航到新路线:

this.props.navigate({id: 'ConfirmBooking', booking: booking}, 'push');

PS:我用的抽屉是react-native-drawer

最佳答案

我假设您的意思是 DrawerLayoutAndroid ( or DrawerLayout ) 作为抽屉组件。

您可以简单地在代码中使用以下行以编程方式在路线更改时打开或关闭抽屉:

this.refs.drawer.openDrawer(); // opens the drawer
this.refs.drawer.closeDrawer(); // closes the drawer

我会将这些调用包含在您的导航功能中,这似乎是最好的地方。

关于javascript - react native : Toggle drawer & navigation bar visibility in routes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35548371/

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