diff --git a/src/actionTypes.js b/src/actionTypes.js index 6f2cb7a7277..ad5e4a971ac 100644 --- a/src/actionTypes.js +++ b/src/actionTypes.js @@ -92,9 +92,10 @@ import type { export type RehydrateAction = { type: typeof REHYDRATE, - // The payload will have `null` at each key where an error was encountered - // in reading the persisted store. - payload: GlobalState | { accounts: null }, + // The payload will be empty on first startup or if the persisted store is + // just missing keys, and will have `null` at each key where an error was + // encountered in reading the persisted store. + payload: GlobalState | { accounts: null } | {}, error: ?Object, }; diff --git a/src/nav/navReducers.js b/src/nav/navReducers.js index 5c4ada992c3..084e8e73d32 100644 --- a/src/nav/navReducers.js +++ b/src/nav/navReducers.js @@ -1,5 +1,6 @@ /* @flow */ import type { + GlobalState, NavigationState, NavAction, RehydrateAction, @@ -22,12 +23,12 @@ import { const initialState = getStateForRoute('loading') || NULL_NAV_STATE; const rehydrate = (state: NavigationState, action: RehydrateAction): NavigationState => { - // If there's an error reading the persisted state, we'll get a - // RehydrateAction with `null` at each key. - if (action.payload.accounts === null) { + if (!action.payload || !action.payload.accounts) { return getStateForRoute('welcome') || state; } - return getInitialNavState(action.payload) || state; + // $FlowFixMe: Flow oddly doesn't see the refinement from the condition above. + const rehydratedState = (action.payload: GlobalState); + return getInitialNavState(rehydratedState) || state; }; const accountSwitch = (state: NavigationState, action: AccountSwitchAction): NavigationState => diff --git a/src/session/sessionReducers.js b/src/session/sessionReducers.js index c74052a40e1..3ffc505c9cd 100644 --- a/src/session/sessionReducers.js +++ b/src/session/sessionReducers.js @@ -69,14 +69,18 @@ const loginSuccess = (state: SessionState, action: LoginSuccessAction): SessionS needsInitialFetch: true, }); -const rehydrate = (state: SessionState, action: RehydrateAction): SessionState => ({ - ...state, - isHydrated: true, - // On rehydration, do an initial fetch if we have access to an account - // (indicated by the presence of an api key). Otherwise, the initial fetch - // will be initiated on loginSuccess. - needsInitialFetch: !!(action.payload.accounts && getAuth(action.payload).apiKey), -}); +const rehydrate = (state: SessionState, action: RehydrateAction): SessionState => { + const payload = { action }; + const haveApiKey = !!(payload && payload.accounts && getAuth(payload).apiKey); + return { + ...state, + isHydrated: true, + // On rehydration, do an initial fetch if we have access to an account + // (indicated by the presence of an api key). Otherwise, the initial fetch + // will be initiated on loginSuccess. + needsInitialFetch: haveApiKey, + }; +}; const realmInit = (state: SessionState, action: RealmInitAction): SessionState => ({ ...state,