Last active
December 8, 2016 13:34
-
-
Save cswl/59ac8c59cecc0f436c5bab02042a485a to your computer and use it in GitHub Desktop.
React native NavigatorExperimental
This file contains 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 { NavigationExperimental, ScrollView, Text, TouchableHighlight } from 'react-native'; | |
const { | |
StateUtils: NavigationStateUtils, | |
} = NavigationExperimental; | |
import styles from './styles' | |
import MyVerySimpleNavigator from './MyVerySimpleNavigator' | |
export default class BleedingEdgeApplication extends Component { | |
constructor(props, context) { | |
super(props, context); | |
this.state = { | |
// This defines the initial navigation state. | |
navigationState: { | |
index: 0, // Starts with first route focused. | |
routes: [{key: 'My Initial Scene'}], // Starts with only one route. | |
}, | |
}; | |
this._onNavigationChange = this._onNavigationChange.bind(this); | |
} | |
_onNavigationChange(type) { | |
// Extract the navigationState from the current state: | |
let {navigationState} = this.state; | |
console.log(type, navigationState) | |
switch (type) { | |
case 'push': | |
// Push a new route, which in our case is an object with a key value. | |
// I am fond of cryptic keys (but seriously, keys should be unique) | |
const route = {key: 'Route-' + Date.now()}; | |
// Use the push reducer provided by NavigationStateUtils | |
navigationState = NavigationStateUtils.push(navigationState, route); | |
break; | |
case 'pop': | |
// Pop the current route using the pop reducer. | |
navigationState = NavigationStateUtils.pop(navigationState); | |
break; | |
} | |
// NavigationStateUtils gives you back the same `navigationState` if nothing | |
// has changed. We will only update state if it has changed. | |
if (this.state.navigationState !== navigationState) { | |
// Always use setState() when setting a new state! | |
this.setState({navigationState}); | |
// If you are new to ES6, the above is equivalent to: | |
// this.setState({navigationState: navigationState}); | |
} | |
} | |
render() { | |
return ( | |
<MyVerySimpleNavigator | |
navigationState={this.state.navigationState} | |
onNavigationChange={this._onNavigationChange} | |
onExit={this._exit} | |
/> | |
); | |
} | |
} |
This file contains 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 { ScrollView, Text } from 'react-native'; | |
import styles from './styles' | |
import TappableRow from './TappableRow' | |
export default class MyVeryComplexScene extends Component { | |
render() { | |
return ( | |
<ScrollView style={styles.scrollView}> | |
<Text style={styles.row}> | |
Route: {this.props.route.key} | |
</Text> | |
<TappableRow | |
text="Tap me to load the next scene" | |
onPress={this.props.onPushRoute} | |
/> | |
<TappableRow | |
text="Tap me to go back" | |
onPress={this.props.onPopRoute} | |
/> | |
</ScrollView> | |
); | |
} | |
} |
This file contains 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 { StyleSheet, PixelRatio } from 'react-native'; | |
export default StyleSheet.create({ | |
navigator: { | |
flex: 1, | |
}, | |
scrollView: { | |
marginTop: 64 | |
}, | |
row: { | |
padding: 15, | |
backgroundColor: 'white', | |
borderBottomWidth: 1 / PixelRatio.get(), | |
borderBottomColor: '#CDCDCD', | |
}, | |
rowText: { | |
fontSize: 17, | |
}, | |
buttonText: { | |
fontSize: 17, | |
fontWeight: '500', | |
}, | |
}); |
This file contains 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 {Text, TouchableHighlight } from 'react-native'; | |
import styles from './styles' | |
export default class TappableRow extends Component { | |
render() { | |
return ( | |
<TouchableHighlight | |
style={styles.row} | |
underlayColor="#D0D0D0" | |
onPress={this.props.onPress}> | |
<Text style={styles.buttonText}> | |
{this.props.text} | |
</Text> | |
</TouchableHighlight> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment