Skip to content

Commit f4f8e9c

Browse files
committed
nav: Stop using react-navigation-redux-helpers.
We've long used Redux to manage our navigation state, with a helper package called react-navigation-redux-helpers. React Navigation has recommended against this for quite some time. In this commit: - Stop using r-n-r-h's `createReduxContainer`, and use React Navigation's own `createAppContainer` instead. - Rename `AppWithNavigation` (arguably better named `ReduxContainer` before this commit) to `AppContainer` - Rename `NavigationService.reduxContainerRef` to `NavigationService.appContainerRef` and change some of `NavigationService`'s implementation details to handle the new shape. - Remove use of `createReactNavigationReduxMiddleware`. - Remove `state.nav`. - Remove `navReducer`. - Adjust types as necessary. - Remove 'nav' from `discardKeys` in src/boot/store.js Fixes: #3804
1 parent 4262acb commit f4f8e9c

File tree

8 files changed

+27
-105
lines changed

8 files changed

+27
-105
lines changed

src/ZulipMobile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import AppEventHandlers from './boot/AppEventHandlers';
1010
import AppDataFetcher from './boot/AppDataFetcher';
1111
import BackNavigationHandler from './nav/BackNavigationHandler';
1212
import InitialNavigationDispatcher from './nav/InitialNavigationDispatcher';
13-
import AppWithNavigation from './nav/AppWithNavigation';
13+
import AppContainer from './nav/AppContainer';
1414
import NavigationService from './nav/NavigationService';
1515

1616
import { initializeSentry } from './sentry';
@@ -29,7 +29,7 @@ export default (): React$Node => (
2929
<ThemeProvider>
3030
<InitialNavigationDispatcher>
3131
<BackNavigationHandler>
32-
<AppWithNavigation ref={NavigationService.reduxContainerRef} />
32+
<AppContainer ref={NavigationService.appContainerRef} />
3333
</BackNavigationHandler>
3434
</InitialNavigationDispatcher>
3535
</ThemeProvider>

src/boot/reducers.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import flags from '../chat/flagsReducer';
1717
import narrows from '../chat/narrowsReducer';
1818
import messages from '../message/messagesReducer';
1919
import mute from '../mute/muteReducer';
20-
import nav from '../nav/navReducer';
2120
import outbox from '../outbox/outboxReducer';
2221
import presence from '../presence/presenceReducer';
2322
import realm from '../realm/realmReducer';
@@ -46,7 +45,6 @@ const reducers = {
4645
messages,
4746
narrows,
4847
mute,
49-
nav,
5048
outbox,
5149
presence,
5250
realm,

src/boot/store.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { Store } from 'redux';
44
import thunkMiddleware from 'redux-thunk';
55
import { createLogger } from 'redux-logger';
66
import createActionBuffer from 'redux-action-buffer';
7-
import { createReactNavigationReduxMiddleware } from 'react-navigation-redux-helpers';
87
import Immutable from 'immutable';
98
import * as Serialize from 'remotedev-serialize';
109
import { persistStore, autoRehydrate } from '../third/redux-persist';
@@ -29,7 +28,7 @@ import createMigration from '../redux-persist-migrate/index';
2928
// prettier-ignore
3029
export const discardKeys: Array<$Keys<GlobalState>> = [
3130
'alertWords', 'caughtUp', 'fetching',
32-
'nav', 'presence', 'session', 'topics', 'typing', 'userStatus',
31+
'presence', 'session', 'topics', 'typing', 'userStatus',
3332
];
3433

3534
/**
@@ -209,10 +208,6 @@ const migrations: { [string]: (GlobalState) => GlobalState } = {
209208
*/
210209
function listMiddleware() {
211210
const result = [
212-
// Allow us to cause navigation by dispatching Redux actions.
213-
// See docs: https://github.com/react-navigation/redux-helpers
214-
createReactNavigationReduxMiddleware((state: GlobalState) => state.nav, 'root'),
215-
216211
// Delay ("buffer") actions until a REHYDRATE action comes through.
217212
// After dispatching the latter, this will go back and dispatch
218213
// all the buffered actions. See docs:

src/nav/AppContainer.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* @flow strict-local */
2+
import { createAppContainer } from 'react-navigation';
3+
import type { NavigationState } from 'react-navigation';
4+
5+
import AppNavigator from './AppNavigator';
6+
7+
export default createAppContainer<NavigationState, { ... }>(AppNavigator);

src/nav/AppWithNavigation.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/nav/NavigationService.js

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,36 @@
22
import React from 'react';
33
import type {
44
NavigationAction,
5-
NavigationNavigatorProps,
65
NavigationState,
7-
NavigationDispatch,
8-
SupportedThemes,
6+
NavigationContainer,
7+
NavigationContainerProps,
98
} from 'react-navigation';
109

11-
type ReduxContainerProps = {
12-
...$Diff<NavigationNavigatorProps<{ ... }, NavigationState>, { navigation: mixed }>,
13-
state: NavigationState,
14-
dispatch: NavigationDispatch,
15-
theme: SupportedThemes | 'no-preference',
16-
};
17-
18-
// Should mimic return type of react-navigation-redux-helpers's
19-
// `createReduxContainer`.
20-
type ReduxContainer = React$Component<ReduxContainerProps>;
21-
22-
// TODO: This will eventually be a ref to the component instance
23-
// created by React Navigation's `createAppContainer`, not
24-
// react-navigation-redux-helpers's `createReduxContainer`.
25-
const reduxContainerRef = React.createRef<ReduxContainer>();
10+
/* prettier-ignore */
11+
const appContainerRef = React.createRef<
12+
React$ElementRef<
13+
NavigationContainer<
14+
NavigationState,
15+
{ ... },
16+
NavigationContainerProps<{ ... }, NavigationState>>>
17+
>();
2618

2719
const getState = (): NavigationState => {
28-
if (reduxContainerRef.current === null) {
29-
throw new Error('Tried to use NavigationService before reduxContainerRef was set.');
20+
if (appContainerRef.current === null) {
21+
throw new Error('Tried to use NavigationService before appContainerRef was set.');
3022
}
31-
return (
32-
reduxContainerRef.current
33-
// $FlowFixMe - how to tell Flow about this method?
34-
.getCurrentNavigation().state
35-
);
23+
return appContainerRef.current.state.nav;
3624
};
3725

3826
const dispatch = (navigationAction: NavigationAction): boolean => {
39-
if (reduxContainerRef.current === null) {
40-
throw new Error('Tried to use NavigationService before reduxContainerRef was set.');
27+
if (appContainerRef.current === null) {
28+
throw new Error('Tried to use NavigationService before appContainerRef was set.');
4129
}
42-
return (
43-
reduxContainerRef.current
44-
// $FlowFixMe - how to tell Flow about this method?
45-
.getCurrentNavigation()
46-
.dispatch(navigationAction)
47-
);
30+
return appContainerRef.current.dispatch(navigationAction);
4831
};
4932

5033
export default {
5134
getState,
5235
dispatch,
53-
reduxContainerRef,
36+
appContainerRef,
5437
};

src/nav/navReducer.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/reduxTypes.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,6 @@ export type NavigationRouteState = {
198198
},
199199
};
200200

201-
export type NavigationState = {|
202-
index: number,
203-
isTransitioning: boolean,
204-
key: string,
205-
routes: NavigationRouteState[],
206-
|};
207-
208201
export type OutboxState = Outbox[];
209202

210203
/**
@@ -360,7 +353,6 @@ export type GlobalState = {|
360353
migrations: MigrationsState,
361354
mute: MuteState,
362355
narrows: NarrowsState,
363-
nav: NavigationState,
364356
outbox: OutboxState,
365357
presence: PresenceState,
366358
realm: RealmState,

0 commit comments

Comments
 (0)