Skip to content

Instantly share code, notes, and snippets.

@dabit3
Last active July 16, 2016 18:53
Show Gist options
  • Save dabit3/c9709864db0878db1debf6ef85bdda7f to your computer and use it in GitHub Desktop.
Save dabit3/c9709864db0878db1debf6ef85bdda7f to your computer and use it in GitHub Desktop.
React Native Navigator Experimental Part 2 — Implementing Redux NavRoot.js
import React, { Component } from 'react'
import Home from './Home'
import About from './About'
import {
BackAndroid,
NavigationExperimental
} from 'react-native'
const {
CardStack: NavigationCardStack
} = NavigationExperimental
class NavRoot extends Component {
constructor (props) {
super(props)
this._renderScene = this._renderScene.bind(this)
this._handleBackAction = this._handleBackAction.bind(this)
}
componentDidMount () {
BackAndroid.addEventListener('hardwareBackPress', this._handleBackAction)
}
componentWillUnmount () {
BackAndroid.removeEventListener('hardwareBackPress', this._handleBackAction)
}
_renderScene (props) {
const { route } = props.scene
if (route.key === 'home') {
return <Home
_handleNavigate={this._handleNavigate.bind(this)} />
}
if (route.key === 'about') {
return <About _goBack={this._handleBackAction.bind(this)} />
}
}
_handleBackAction () {
if (this.props.navigation.index === 0) {
return false
}
this.props.popRoute()
return true
}
_handleNavigate (action) {
switch (action && action.type) {
case 'push':
this.props.pushRoute(action.route)
return true
case 'back':
case 'pop':
return this._handleBackAction()
default:
return false
}
}
render () {
return (
<NavigationCardStack
direction='vertical'
navigationState={this.props.navigation}
onNavigate={this._handleNavigate.bind(this)}
renderScene={this._renderScene} />
)
}
}
export default NavRoot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment