Skip to content

Instantly share code, notes, and snippets.

@richardgill
Created December 2, 2016 16:35
Show Gist options
  • Save richardgill/8eb1220ad97c4271a6e6b3ec898b9e65 to your computer and use it in GitHub Desktop.
Save richardgill/8eb1220ad97c4271a6e6b3ec898b9e65 to your computer and use it in GitHub Desktop.
Gives you an idea of the sorts of things we needed from a general purpose navigator
import React, { Component } from 'react'
import { Navigator, StatusBar, View } from 'react-native'
import customNavigatorScenes from '../../common/customNavigatorScenes'
import style from './style'
import ErrorListener from './errorListener'
import NetworkConnectivity from '../networkConnectivity'
import _ from 'lodash'
import { brandPrimary } from '../../styleVariables'
import { reduxComponentName } from '../../common/reduxComponentName'
const navigatorComponents = {}
let navigatorInstance
class Navigator1 extends Component {
onDidFocus = (route) => {
if (route.resetRouteStack) {
this.refs.navigator.immediatelyResetRouteStack(route.resetRouteStack)
}
const componentInstance = navigatorComponents[reduxComponentName(route.component)]
if (componentInstance) {
if (!_.isUndefined(componentInstance.componentDidFocus)) {
componentInstance.componentDidFocus()
}
if (!_.isUndefined(componentInstance.getWrappedInstance) && !_.isUndefined(componentInstance.getWrappedInstance().componentDidFocus)) {
componentInstance.getWrappedInstance().componentDidFocus()
}
}
}
render() {
return (
<View style={style.container}>
<NetworkConnectivity />
<StatusBar hidden={this.props.hideStatusBar} backgroundColor={brandPrimary} />
<Navigator
ref="navigator"
initialRoute={this.props.initialRoute}
renderScene={(route, navigator) => {
if (this.props.navigatorRef) {
navigatorInstance = navigator
this.props.navigatorRef(navigator)
}
const RouteComponent = route.component
return (
<View style={style.container}>
<ErrorListener navigator={navigator} />
<RouteComponent
navigator={navigator}
{...route.passProps}
ref={(view) => {
navigatorComponents[reduxComponentName(RouteComponent)] = view
}}
/>
</View>
)
}}
onDidFocus={this.onDidFocus}
configureScene={(route) => {
this.props.sceneChanged(route, navigatorInstance)
if (route.sceneConfig) {
return route.sceneConfig
}
return customNavigatorScenes.HorizontalJump
}}
/>
</View>
)
}
}
/* eslint-disable no-unused-vars */
Navigator1.defaultProps = { hideStatusBar: false, sceneChanged: (route, navigator) => route }
export default Navigator1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment