Skip to content

Instantly share code, notes, and snippets.

@arendjr
Created April 17, 2016 14:43
Show Gist options
  • Save arendjr/45b9cd3e96eed335a6ac41e125354926 to your computer and use it in GitHub Desktop.
Save arendjr/45b9cd3e96eed335a6ac41e125354926 to your computer and use it in GitHub Desktop.
import _ from 'lodash';
import React, { Component, Navigator, PropTypes, StyleSheet, View } from 'react-native';
import NavigationBar from './navigation-bar';
export default class DeclarativeNavigator extends Component {
componentWillUpdate(nextProps) {
const currentStack = this.props.routeStack;
const currentLength = currentStack.length;
const newStack = nextProps.routeStack;
const newLength = newStack.length;
if (newLength === currentLength) {
for (let i = 0; i < newLength; i++) {
if (newStack[i] !== currentStack[i]) {
this.navigator.replaceAtIndex(newStack[i], i);
}
}
} else {
let i = currentLength - 1;
for (; i >= 0; i--) {
if (i >= newLength || currentStack[i] !== newStack[i]) {
this.navigator.pop();
}
}
for (; i < newLength; i++) {
this.navigator.push(newStack[i]);
}
}
}
render() {
const { renderScene, routeStack } = this.props;
const { title } = _.last(routeStack);
return (
<View style={styles.container}>
{title ? <NavigationBar title={title} /> : null}
<Navigator initialRouteStack={routeStack}
ref={ref => this.navigator = ref}
renderScene={renderScene}
sceneStyle={styles.scene} />
</View>
);
}
}
DeclarativeNavigator.propTypes = {
renderScene: PropTypes.func.isRequired,
routeStack: PropTypes.array.isRequired
};
const styles = StyleSheet.create({
container: {
flex: 1
},
scene: {
flex: 1
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment