Skip to content

Instantly share code, notes, and snippets.

@ronnyhartenstein
Last active July 17, 2023 10:14
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ronnyhartenstein/1ef30c90f530f99430969925198d6970 to your computer and use it in GitHub Desktop.
Save ronnyhartenstein/1ef30c90f530f99430969925198d6970 to your computer and use it in GitHub Desktop.
React Navigation: Parallel Navigators in React Native in a nutshell
import React from 'react'
import { AppRegistry } from 'react-native'
import setup from './setup'
AppRegistry.registerComponent('ReactNavigationTest', setup)
import { DrawerNavigator } from 'react-navigation'
import Overview from './screenOverview'
import Profile from './screenProfile'
import Settings from './screenSettings'
const MainNavigator = DrawerNavigator({
Overview: { screen: Overview },
Profile: { screen: Profile },
Settings: { screen: Settings },
}, {
initialRouteName: 'Overview',
})
export default MainNavigator
import { StackNavigator } from 'react-navigation'
import Register from './screenRegister'
import Login from './screenLogin'
import PwdForget from './screenPwdforget'
import Tour from './screenTour'
const OnboardingNavigator = StackNavigator({
Login: { screen: Login },
Register: { screen: Register },
PwdForgot: { screen: PwdForgot },
Tour: { screen: Tour },
}, {
initialRouteName: 'Login'
})
export default OnboardingNavigator
import React, { Component } from 'react'
import { connect } from 'react-redux'
import OnboardingNavigator from './navigationOnboarding'
import MainNavigator from './navigationMain'
class Navigator extends Component {
render() {
return this.props.login ? <MainNavigator/> : <OnboardingNavigator/>
}
}
const mapStateToProps = state => ({ login: state })
export default connect(mapStateToProps, {}})(Navigator)
import React from 'react'
import { Text, View, Button } from 'react-native'
import { connect } from 'react-redux'
class Login extends React.Component {
render() {
return (
<View>
<Text>Name, Password</Text>
<Button onPress={() => this.props.login()} title="Login" />
<Button onPress={() => this.props.navigation.navigate('Register')} title="Register" />
<Button onPress={() => this.props.navigation.navigate('PwdForgot')} title="Forget Password" />
<Button onPress={() => this.props.navigation.navigate('Tour')} title="Tour" />
</View>
)
}
}
function bindActions(dispatch) {
return {
login: () => dispatch({type:'LOGIN'}),
}
}
const mapStateToProps = state => ({})
export default connect(mapStateToProps, bindActions)(Login)
import React from 'react';
import { Text, View, Button } from 'react-native';
import { connect } from 'react-redux'
class Overview extends React.Component {
static navigationOptions = {
drawer: () => ({
label: 'My Overview',
})
}
render() {
return (
<View>
<Text>Something special</Text>
<Button onPress={() => this.props.navigation.navigate('DrawerOpen')} title="Menu" />
<Button onPress={() => this.props.logout()} title="Logout" />
</View>
);
}
}
function bindActions(dispatch) {
return {
logout: () => dispatch({type:'LOGOUT'}),
}
}
const mapStateToProps = state => ({})
export default connect(mapStateToProps, bindActions)(Ueberblick)
import React, { Component } from 'react'
import { Provider } from 'react-redux'
import Navigator from './navigator'
import { createStore } from 'redux';
function loginReducer(state = false, action) {
switch (action.type) {
case 'LOGIN': return true
case 'LOGOUT': return false
default: return state
}
}
const store = createStore(loginReducer, false);
function setup():React.Component {
class Root extends Component {
constructor() {
super()
this.state = { store }
}
render() {
return (
<Provider store={this.state.store}>
<Navigator />
</Provider>
)
}
}
return Root
}
export default setup
@kcfgl
Copy link

kcfgl commented May 10, 2017

@ronnyhartenstein This is excellent. Clean, straightforward and flexible. I'm wondering why it isnt more widely accepted as a suitable solution? Have you found any shortcomings i.e. deep linking, navigating based on push notification payloads (after verifying they are logged in), etc? Thanks again!

@nfabian13
Copy link

how would you logout from the MainNavigator and redirect to the Login screen found in the OnboardingNavigator using redux?

@TheRusskiy
Copy link

Sometimes I am getting weird errors when transitioning between screens this way. Had to add setTimeout which is beyond hackish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment