fix: handle partial initial state better when rehydrating

This commit is contained in:
satyajit.happy
2019-08-16 03:13:48 +05:30
committed by Satyajit Sahoo
parent ca985bb96a
commit 8ed54dace4
8 changed files with 165 additions and 58 deletions

View File

@@ -68,6 +68,25 @@ export default function DrawerRouter(
};
},
getRehydratedState(partialState, { routeNames, routeParamList }) {
if (!partialState.stale) {
return partialState as DrawerNavigationState;
}
const state = router.getRehydratedState(partialState, {
routeNames,
routeParamList,
});
return {
...state,
isDrawerOpen:
typeof partialState.isDrawerOpen === 'boolean'
? partialState.isDrawerOpen
: false,
};
},
getStateForRouteFocus(state, key) {
const index = state.routes.findIndex(r => r.key === key);

View File

@@ -5,6 +5,7 @@ import {
Router,
BaseRouter,
DefaultRouterOptions,
Route,
} from '@navigation-ex/core';
export type StackActionType =
@@ -47,36 +48,69 @@ export default function StackRouter(options: StackRouterOptions) {
...BaseRouter,
getInitialState({ routeNames, routeParamList }) {
const index =
options.initialRouteName === undefined
? 0
: routeNames.indexOf(options.initialRouteName);
const initialRouteName =
options.initialRouteName !== undefined
? options.initialRouteName
: routeNames[0];
return {
key: `stack-${shortid()}`,
index,
index: 0,
routeNames,
routes: routeNames.slice(0, index + 1).map(name => ({
name,
key: `${name}-${shortid()}`,
params: routeParamList[name],
})),
routes: [
{
key: `${initialRouteName}-${shortid()}`,
name: initialRouteName,
params: routeParamList[initialRouteName],
},
],
};
},
getRehydratedState({ routeNames, partialState }) {
getRehydratedState(partialState, { routeNames, routeParamList }) {
let state = partialState;
if (state.stale) {
state = {
...state,
stale: false,
routeNames,
key: `stack-${shortid()}`,
};
if (!state.stale) {
return state as StackNavigationState;
}
return state;
const routes = state.routes
.filter(route => routeNames.includes(route.name))
.map(
route =>
({
...route,
key: route.key || `${route.name}-${shortid()}`,
params:
routeParamList[route.name] !== undefined
? {
...routeParamList[route.name],
...route.params,
}
: route.params,
} as Route<string>)
);
if (routes.length === 0) {
const initialRouteName =
options.initialRouteName !== undefined
? options.initialRouteName
: routeNames[0];
routes.push({
key: `${initialRouteName}-${shortid()}`,
name: initialRouteName,
params: routeParamList[initialRouteName],
});
}
return {
stale: false,
key: `stack-${shortid()}`,
index: routes.length - 1,
routeNames,
routes,
};
},
getStateForRouteNamesChange(state, { routeNames }) {

View File

@@ -71,19 +71,46 @@ export default function TabRouter({
};
},
getRehydratedState({ routeNames, partialState }) {
getRehydratedState(partialState, { routeNames, routeParamList }) {
let state = partialState;
if (state.stale) {
state = {
...state,
stale: false,
routeNames,
key: `tab-${shortid()}`,
};
if (!state.stale) {
return state as TabNavigationState;
}
return state;
const routes = routeNames.map(name => {
const route = state.routes.find(r => r.name === name);
return {
name,
key: route && route.key ? route.key : `${name}-${shortid()}`,
params:
routeParamList[name] !== undefined
? {
...routeParamList[name],
...(route ? route.params : undefined),
}
: route
? route.params
: undefined,
};
});
const routeKeyHistory = state.routeKeyHistory
? state.routeKeyHistory.filter(key => routes.find(r => r.key === key))
: [];
return {
stale: false,
key: `tab-${shortid()}`,
index:
typeof state.index === 'number' && state.index < routes.length
? state.index
: 0,
routeNames,
routeKeyHistory,
routes,
};
},
getStateForRouteNamesChange(state, { routeNames, routeParamList }) {