Files
react-native/Libraries/NavigationExperimental/NavigationRootContainer.js
Eric Vicenti 876ecb291f Adopt NavigationExperimental in UIExplorer
Summary:Use the new Navigation library to make the UIExplorer navigation more flexible.

Deep linking examples are coming soon (hint: we just need to convert URIs to UIExplorerActions!)

Reviewed By: javache

Differential Revision: D2798050

fb-gh-sync-id: c7775393e2d7a30a161d0770192309567dcc8b0c
shipit-source-id: c7775393e2d7a30a161d0770192309567dcc8b0c
2016-02-22 16:17:06 -08:00

111 lines
2.7 KiB
JavaScript

/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule NavigationRootContainer
* @flow
*/
'use strict';
const AsyncStorage = require('AsyncStorage');
const React = require('React');
const BackAndroid = require('BackAndroid');
const Platform = require('Platform');
import type {
NavigationAction,
NavigationState,
NavigationReducer
} from 'NavigationStateUtils';
export type NavigationRenderer = (
navigationState: NavigationState,
onNavigate: Function
) => ReactElement;
export type BackAction = {
type: 'BackAction';
};
function getBackAction(): BackAction {
return { type: 'BackAction' };
}
type Props = {
renderNavigation: NavigationRenderer;
reducer: NavigationReducer;
persistenceKey: ?string;
initialAction: NavigationAction;
};
class NavigationRootContainer extends React.Component {
props: Props;
constructor(props: Props) {
super(props);
this.handleNavigation = this.handleNavigation.bind(this);
let navState = null;
if (!this.props.persistenceKey) {
navState = this.props.reducer(null, props.initialAction);
}
this.state = { navState };
}
componentDidMount() {
if (this.props.persistenceKey) {
AsyncStorage.getItem(this.props.persistenceKey, (err, storedString) => {
if (err || !storedString) {
this.setState({
navState: this.props.reducer(null, this.props.initialAction),
});
return;
}
this.setState({
navState: JSON.parse(storedString),
});
});
}
}
getChildContext(): Object {
return {
onNavigate: this.handleNavigation,
};
}
handleNavigation(action: Object): boolean {
const navState = this.props.reducer(this.state.navState, action);
if (navState === this.state.navState) {
return false;
}
this.setState({
navState,
});
if (this.props.persistenceKey) {
AsyncStorage.setItem(this.props.persistenceKey, JSON.stringify(navState));
}
return true;
}
render(): ReactElement {
const navigation = this.props.renderNavigation(
this.state.navState,
this.handleNavigation
);
return navigation;
}
}
NavigationRootContainer.childContextTypes = {
onNavigate: React.PropTypes.func,
};
NavigationRootContainer.defaultProps = {
initialAction: {
type: 'NavigationRootContainerInitialAction',
},
};
NavigationRootContainer.getBackAction = getBackAction;
module.exports = NavigationRootContainer;