Skip to content

Commit

Permalink
Kill NavigationReducers
Browse files Browse the repository at this point in the history
Summary:
For navigation actions at high level, reducers from NavigationReducers does not
know anything about the app-specific state thus people won't use these reducers.
Instead, people should build their own reducers.

There are a lot of good libraries available that help people to reducing things if that's
what they really need.

At the low level, for navigation state changes that don't involve app-specific state,
`NavigationStateUtils` should server that kind of need.

`NavigationReducers` serves little benefit cause it does not know the app state, it does
not know how to traverse the navigation states which can be a tree, a list or a map.

That said, we hold no interest in owning in the core navigation library.

Reviewed By: ericvicenti

Differential Revision: D3372910

fbshipit-source-id: 797382b46e7d64b7ad578b51dd37e2b941faa83d
  • Loading branch information
Hedger Wang authored and Facebook Github Bot 4 committed Jun 8, 2016
1 parent bb9ed2d commit 3a8b50a
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 538 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class YourApplication extends React.Component {
switch (type) { switch (type) {
case 'push': case 'push':
// push a new route. // push a new route.
const route = {key: Date.now()}; const route = {key: 'route-' + Date.now()};
navigationState = NavigationStateUtils.push(navigationState, route); navigationState = NavigationStateUtils.push(navigationState, route);
break; break;


Expand Down
57 changes: 51 additions & 6 deletions Examples/UIExplorer/UIExplorerNavigationReducer.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,27 +22,72 @@
*/ */
'use strict'; 'use strict';


const React = require('react');
const ReactNative = require('react-native'); const ReactNative = require('react-native');
// $FlowFixMe : This is a platform-forked component, and flow seems to only run on iOS? // $FlowFixMe : This is a platform-forked component, and flow seems to only run on iOS?
const UIExplorerList = require('./UIExplorerList'); const UIExplorerList = require('./UIExplorerList');

const { const {
NavigationExperimental, NavigationExperimental,
} = ReactNative; } = ReactNative;


const { const {
Reducer: NavigationReducer, StateUtils: NavigationStateUtils,
} = NavigationExperimental; } = NavigationExperimental;
const StackReducer = NavigationReducer.StackReducer;


import type {NavigationState} from 'NavigationTypeDefinition'; import type {NavigationState} from 'NavigationTypeDefinition';


import type {UIExplorerAction} from './UIExplorerActions';

export type UIExplorerNavigationState = { export type UIExplorerNavigationState = {
externalExample: ?string; externalExample: ?string;
stack: NavigationState; stack: NavigationState;
}; };


const defaultGetReducerForState = (initialState) => (state) => state || initialState;

function StackReducer({initialState, getReducerForState, getPushedReducerForAction}: any): Function {
const getReducerForStateWithDefault = getReducerForState || defaultGetReducerForState;
return function (lastState: ?NavigationState, action: any): NavigationState {
if (!lastState) {
return initialState;
}
const lastParentState = NavigationStateUtils.getParent(lastState);
if (!lastParentState) {
return lastState;
}

const activeSubState = lastParentState.routes[lastParentState.index];
const activeSubReducer = getReducerForStateWithDefault(activeSubState);
const nextActiveState = activeSubReducer(activeSubState, action);
if (nextActiveState !== activeSubState) {
const nextChildren = [...lastParentState.routes];
nextChildren[lastParentState.index] = nextActiveState;
return {
...lastParentState,
routes: nextChildren,
};
}

const subReducerToPush = getPushedReducerForAction(action, lastParentState);
if (subReducerToPush) {
return NavigationStateUtils.push(
lastParentState,
subReducerToPush(null, action)
);
}

switch (action.type) {
case 'back':
case 'BackAction':
if (lastParentState.index === 0 || lastParentState.routes.length === 1) {
return lastParentState;
}
return NavigationStateUtils.pop(lastParentState);
}

return lastParentState;
};
}

const UIExplorerStackReducer = StackReducer({ const UIExplorerStackReducer = StackReducer({
getPushedReducerForAction: (action, lastState) => { getPushedReducerForAction: (action, lastState) => {
if (action.type === 'UIExplorerExampleAction' && UIExplorerList.Modules[action.openExample]) { if (action.type === 'UIExplorerExampleAction' && UIExplorerList.Modules[action.openExample]) {
Expand Down Expand Up @@ -106,7 +151,7 @@ function UIExplorerNavigationReducer(lastState: ?UIExplorerNavigationState, acti
return { return {
externalExample: null, externalExample: null,
stack: newStack, stack: newStack,
} };
} }
return lastState; return lastState;
} }
Expand Down
2 changes: 0 additions & 2 deletions Libraries/NavigationExperimental/NavigationExperimental.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ const NavigationCard = require('NavigationCard');
const NavigationCardStack = require('NavigationCardStack'); const NavigationCardStack = require('NavigationCardStack');
const NavigationHeader = require('NavigationHeader'); const NavigationHeader = require('NavigationHeader');
const NavigationPropTypes = require('NavigationPropTypes'); const NavigationPropTypes = require('NavigationPropTypes');
const NavigationReducer = require('NavigationReducer');
const NavigationStateUtils = require('NavigationStateUtils'); const NavigationStateUtils = require('NavigationStateUtils');
const NavigationTransitioner = require('NavigationTransitioner'); const NavigationTransitioner = require('NavigationTransitioner');


const NavigationExperimental = { const NavigationExperimental = {
// Core // Core
StateUtils: NavigationStateUtils, StateUtils: NavigationStateUtils,
Reducer: NavigationReducer,


// Views // Views
AnimatedView: NavigationAnimatedView, AnimatedView: NavigationAnimatedView,
Expand Down
41 changes: 0 additions & 41 deletions Libraries/NavigationExperimental/Reducer/NavigationFindReducer.js

This file was deleted.

24 changes: 0 additions & 24 deletions Libraries/NavigationExperimental/Reducer/NavigationReducer.js

This file was deleted.

102 changes: 0 additions & 102 deletions Libraries/NavigationExperimental/Reducer/NavigationStackReducer.js

This file was deleted.

103 changes: 0 additions & 103 deletions Libraries/NavigationExperimental/Reducer/NavigationTabsReducer.js

This file was deleted.

Loading

3 comments on commit 3a8b50a

@steida
Copy link

@steida steida commented on 3a8b50a Jun 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@xing-zheng
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @hedgerwang , I appreciate the NavigationExperimental component very much, Could you tell me we where can I found the latest document about this component, I found the navigation-rfc project is not maintained.

@hedgerwang
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xing-zheng : we're working on the documentation and they should be ready in few weeks.

Please sign in to comment.