refactor: add separate method for rehydration and fix types

This commit is contained in:
satyajit.happy
2019-07-14 18:42:30 +02:00
parent 43bc406c00
commit db6fe6bb1e
10 changed files with 202 additions and 118 deletions

View File

@@ -55,27 +55,27 @@ export type StackNavigationProp<
const StackRouter: Router<CommonAction | Action> = {
getInitialState({
screens,
partialState,
initialRouteName = Object.keys(screens)[0],
routeNames,
initialRouteName = routeNames[0],
initialParamsList,
}) {
const routeNames = Object.keys(screens);
const index = routeNames.indexOf(initialRouteName);
return {
key: `stack-${shortid()}`,
index,
routeNames,
routes: routeNames.slice(0, index + 1).map(name => ({
name,
key: `${name}-${shortid()}`,
params: initialParamsList[name],
})),
};
},
getRehydratedState({ routeNames, partialState }) {
let state = partialState;
if (state === undefined) {
const index = routeNames.indexOf(initialRouteName);
state = {
index,
routes: routeNames.slice(0, index + 1).map(name => ({
name,
key: `${name}-${shortid()}`,
params: screens[name].initialParams,
})),
};
}
if (state.routeNames === undefined || state.key === undefined) {
state = {
...state,