mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-02-12 17:31:06 +08:00
refactor: drop getStateForChildUpdate in favor of getStateForRouteFocus (#15)
This commit is contained in:
committed by
Michał Osadnik
parent
3d8ba13135
commit
44b2ace9ee
@@ -96,6 +96,20 @@ const StackRouter: Router<CommonAction | Action> = {
|
||||
};
|
||||
},
|
||||
|
||||
getStateForRouteFocus(state, key) {
|
||||
const index = state.routes.findIndex(r => r.key === key);
|
||||
|
||||
if (index === -1 || index === state.index) {
|
||||
return state;
|
||||
}
|
||||
|
||||
return {
|
||||
...state,
|
||||
index,
|
||||
routes: state.routes.slice(0, index + 1),
|
||||
};
|
||||
},
|
||||
|
||||
getStateForAction(state, action) {
|
||||
switch (action.type) {
|
||||
case 'PUSH':
|
||||
@@ -219,27 +233,6 @@ const StackRouter: Router<CommonAction | Action> = {
|
||||
}
|
||||
},
|
||||
|
||||
getStateForChildUpdate(state, { update, focus, key }) {
|
||||
const index = state.routes.findIndex(r => r.key === key);
|
||||
|
||||
if (index === -1) {
|
||||
return state;
|
||||
}
|
||||
|
||||
return {
|
||||
...state,
|
||||
index: focus ? index : state.index,
|
||||
routes: focus
|
||||
? [
|
||||
...state.routes.slice(0, index),
|
||||
{ ...state.routes[index], state: update },
|
||||
]
|
||||
: state.routes.map((route, i) =>
|
||||
i === index ? { ...route, state: update } : route
|
||||
),
|
||||
};
|
||||
},
|
||||
|
||||
shouldActionPropagateToChildren(action) {
|
||||
return action.type === 'NAVIGATE';
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user