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
@ronnyhartenstein
Copy link
Author

Example for a parallel React Navigation onboarding StackNavigation next to a main app DrawerNavigation.

Targets these issues:

@hecbuma
Copy link

hecbuma commented Apr 13, 2017

HI @ronnyhartenstein this is great, I'm trying to use this gist for a sample app I'm building but I can't get it to work. I'm getting this error

ExceptionsManager.js:63 setup(...): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.

so far I'm using your code, and then I'll try to make my modifications.

Do you know what's going on or what am I missing here?

@hecbuma
Copy link

hecbuma commented Apr 14, 2017

I removed the wrapped function setup and at the end I'm export Root class, I'm learning react-native/react so I'm like
b7e

at least it's working, if I'm doing something wrong please advice. Thanks again.

@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