Last active
July 17, 2023 10:14
-
-
Save ronnyhartenstein/1ef30c90f530f99430969925198d6970 to your computer and use it in GitHub Desktop.
React Navigation: Parallel Navigators in React Native in a nutshell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import { AppRegistry } from 'react-native' | |
import setup from './setup' | |
AppRegistry.registerComponent('ReactNavigationTest', setup) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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!
how would you logout from the MainNavigator and redirect to the Login screen found in the OnboardingNavigator using redux?
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
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

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