Nesta apresentação nós veremos um benchmark entre os principais componentes de navegação do React Native e as boas práticas para a implementação de uma árvore de navegação para as telas dos módulos do aplicativo utilizando React Navigation.
2. MODULAR NAVIGATION WITH REACT NAVIGATION
ROADMAP
▸ 1- Overview
▸ 2- Navigation Boilerplate
▸ 3- Nested Navigation
▸ 4- Go Back to Navigation Root
▸ 5- EXTRA - Status Bar Height Adjustment
6. REACT NAVIGATION OVERVIEW
INTRODUCTION
▸ React Navigation is the result of a collaboration between
developers from Facebook, Expo and the React community at
large: it replaces and improves upon several navigation libraries
in the ecosystem, including Ex-Navigation, React Native's
Navigator and NavigationExperimental components.
▸ Release in late January 2017.
▸ Officially recommended navigation library for React Native in
September 2017.
▸ Expo has expressed that React Navigation v1.0 is priority.
26. GO BACK TO NAVIGATION ROOT
NAVIGATION ACTIONS
▸ Navigate - Navigate to another route.
▸ Reset - Replace current state with a new state.
▸ Back - Go back to previous state.
▸ Set Params - Set Params for given route.
▸ Init - Used to initialize first if state is undefined.
import { NavigationActions } from ‘react-navigation’;
this.props.navigation.dispatch(
NavigationActions.reset({ params... })
);
27. GO BACK TO NAVIGATION ROOT
GO TO HOME AFTER LOG ON
<Button
raised
title='Login'
onPress={() =>
this.props.navigation.root
.navigate(‘Main’)
}
/>
28. GO BACK TO NAVIGATION ROOT
GO TO LOGIN AFTER LOG OFF
contentComponent: props => (
<ScrollView
...
<Button
title='Logout'
onPress={() => {
this.props.navigation.root
.dispatch(
NavigationActions.reset({
index: 0,
actions: []
})
);
this.props.navigation.root
.navigate(‘Auth');
}}
/>
...
</ScrollView>
)
29. GO BACK TO NAVIGATION ROOT
GO TO WELCOME AFTER YOU LEAVE
contentComponent: props => (
<ScrollView
...
<Button
title='Logout'
onPress={() => {
this.props.navigation.root
.dispatch(
NavigationActions.reset({
index: 0,
actions: []
})
);
this.props.navigation.root
.navigate(‘Welcome’);
}}
/>
...
</ScrollView>
)
30. MODULAR NAVIGATION WITH REACT NAVIGATION
EXTRA - ANDROID STATUS BAR HEIGHT ADJUSTMENT
import { Platform } from 'react-native';
import Expo from 'expo';
export default class HomeTabNavigation
extends React.Component {
render() {
const HomeTabNavigator = TabNavigator({
...
}, {
...
tabBarOptions: {
labelStyle: {
fontSize: 10
},
showIcon: true,
tabStyle: {
marginTop: Platform.OS === 'ios' ? 0
: Expo.Constants.statusBarHeight
}
}
});
return (
<HomeTabNavigator />
);
}
}