Skip to content

Instantly share code, notes, and snippets.

@cswl
Last active December 8, 2016 13:34
Show Gist options
  • Save cswl/59ac8c59cecc0f436c5bab02042a485a to your computer and use it in GitHub Desktop.
Save cswl/59ac8c59cecc0f436c5bab02042a485a to your computer and use it in GitHub Desktop.
React native NavigatorExperimental
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}
/>
);
}
}
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>
);
}
}
import React, { Component } from 'react';
import { NavigationExperimental } from 'react-native';
const {
CardStack: NavigationCardStack,
StateUtils: NavigationStateUtils,
} = NavigationExperimental;
import styles from './styles'
import MyVeryComplexScene from './MyVeryComplexScene'
export default class MyVerySimpleNavigator extends Component {
// This sets up the methods (e.g. Pop, Push) for navigation.
constructor(props, context) {
super(props, context);
this._onPushRoute = this.props.onNavigationChange.bind(null, 'push');
this._onPopRoute = this.props.onNavigationChange.bind(null, 'pop');
this._renderScene = this._renderScene.bind(this);
}
// Now we finally get to use the `NavigationCardStack` to render the scenes.
render() {
return (
<NavigationCardStack
onNavigateBack={this._onPopRoute}
navigationState={this.props.navigationState}
renderScene={this._renderScene}
style={styles.navigator}
/>
);
}
// Render a scene for route.
// The detailed spec of `sceneProps` is defined at `NavigationTypeDefinition`
// as type `NavigationSceneRendererProps`.
// Here you could choose to render a different component for each route, but
// we'll keep it simple.
_renderScene(sceneProps) {
return (
<MyVeryComplexScene
route={sceneProps.scene.route}
onPushRoute={this._onPushRoute}
onPopRoute={this._onPopRoute}
onExit={this.props.onExit}
/>
);
}
}
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',
},
});
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