Skip to content

Commit 21c64e7

Browse files
committed
navReducer: Use getNavigationRoutes selector, for simplicity.
Also add a comment, with Greg's description [1] of what's going on here. [1] zulip#4274 (comment)
1 parent 67ca157 commit 21c64e7

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/nav/__tests__/navReducer-test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ import deepFreeze from 'deep-freeze';
22

33
import { INITIAL_FETCH_COMPLETE } from '../../actionConstants';
44
import navReducer, { getStateForRoute } from '../navReducer';
5+
import NavigationService from '../NavigationService';
56

67
describe('navReducer', () => {
78
describe('INITIAL_FETCH_COMPLETE', () => {
89
test('do not mutate navigation state if already at the same route', () => {
10+
NavigationService.getState = jest.fn().mockReturnValue(
11+
deepFreeze({
12+
index: 0,
13+
routes: [{ routeName: 'main' }],
14+
}),
15+
);
916
const prevState = getStateForRoute('main');
1017

1118
const action = deepFreeze({

src/nav/navReducer.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import type { NavigationAction } from 'react-navigation';
33

44
import type { NavigationState, Action } from '../types';
5+
import { getNavigationRoutes } from './navSelectors';
56
import AppNavigator from './AppNavigator';
67
import { INITIAL_FETCH_COMPLETE } from '../actionConstants';
78

@@ -29,7 +30,10 @@ export const initialState = getStateForRoute('loading');
2930
export default (state: NavigationState = initialState, action: Action): NavigationState => {
3031
switch (action.type) {
3132
case INITIAL_FETCH_COMPLETE:
32-
return state.routes[0].routeName === 'main' ? state : getStateForRoute('main');
33+
// If we're anywhere in the normal UI of the app, then remain
34+
// where we are. Only reset the nav state if we're elsewhere,
35+
// and in that case, go to the main screen.
36+
return getNavigationRoutes()[0].routeName === 'main' ? state : getStateForRoute('main');
3337

3438
default: {
3539
// The `react-navigation` libdef says this only takes a NavigationAction,

src/nav/navSelectors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import NavigationService from './NavigationService';
55

66
export const getNavState = (): NavigationState => NavigationService.getState();
77

8-
const getNavigationRoutes = () => getNavState().routes;
8+
export const getNavigationRoutes = () => getNavState().routes;
99

1010
const getNavigationIndex = () => getNavState().index;
1111

0 commit comments

Comments
 (0)