Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 84f1095

Browse files
committed
Allow calling createReduxContainer before createReactNavigationReduxMiddleware
1 parent aec15a9 commit 84f1095

2 files changed

Lines changed: 12 additions & 28 deletions

File tree

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ const appReducer = combineReducers({
5555
...
5656
});
5757

58-
// Note: createReactNavigationReduxMiddleware must be run before createReduxContainer
5958
const middleware = createReactNavigationReduxMiddleware(
6059
state => state.nav,
6160
);
@@ -107,7 +106,6 @@ function createReduxContainer(
107106
```
108107

109108
* Returns a HOC (higher-order component) that wraps your root navigator.
110-
* `createReactNavigationReduxMiddleware` must be called before this one!
111109
* Param `navigator` is your root navigator (React component).
112110
* Param `key` needs to be consistent with the call to `createReactNavigationReduxMiddleware` above. You can leave it out if you only have one store.
113111
* Returns a component to use in place of your root navigator. Pass it `state` and `dispatch` props that you get via `react-redux`'s `connect`.

src/middleware.js

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ import invariant from 'invariant';
1616
import { initAction } from './reducer';
1717

1818
const reduxSubscribers = new Map();
19+
function getReduxSubscribers(key: string): Set<NavigationEventCallback> {
20+
let subscribers = reduxSubscribers.get(key);
21+
if (!subscribers) {
22+
subscribers = new Set();
23+
reduxSubscribers.set(key, subscribers);
24+
}
25+
return subscribers;
26+
}
1927

2028
// screenProps are a legacy concept in React Navigation,
2129
// and should not be used in redux apps
@@ -26,16 +34,12 @@ function createReactNavigationReduxMiddleware<State: {}>(
2634
navStateSelector: (state: State) => NavigationState,
2735
key?: string = "root",
2836
): Middleware<State, *, *> {
29-
reduxSubscribers.set(key, new Set());
3037
return store => next => action => {
3138
const oldState = store.getState();
3239
const result = next(action);
3340
const newState = store.getState();
34-
const subscribers = reduxSubscribers.get(key);
35-
invariant(subscribers, `subscribers set should exist for ${key}`);
3641
triggerAllSubscribers(
3742
key,
38-
subscribers,
3943
{
4044
type: 'action',
4145
action,
@@ -49,12 +53,9 @@ function createReactNavigationReduxMiddleware<State: {}>(
4953

5054
const delayedTriggers = new Map();
5155

52-
function triggerAllSubscribers(
53-
key: string,
54-
subscribers: Set<NavigationEventCallback>,
55-
payload: NavigationEventPayload,
56-
) {
57-
const trigger = () => subscribers.forEach(subscriber => subscriber(payload));
56+
function triggerAllSubscribers(key: string, payload: NavigationEventPayload) {
57+
const trigger =
58+
() => getReduxSubscribers(key).forEach(subscriber => subscriber(payload));
5859
if (
5960
!payload.action.hasOwnProperty('type') ||
6061
!payload.action.type.startsWith("Navigation") ||
@@ -87,16 +88,8 @@ function createDidUpdateCallback(key: string) {
8788
}
8889

8990
function initializeListeners(key: string, state: NavigationState) {
90-
const subscribers = reduxSubscribers.get(key);
91-
invariant(
92-
subscribers,
93-
"Before calling `createReduxContainer`, please call " +
94-
"`createReactNavigationReduxMiddleware`, so that we know " +
95-
"when to trigger your listener.",
96-
);
9791
triggerAllSubscribers(
9892
key,
99-
subscribers,
10093
{
10194
type: 'action',
10295
action: initAction,
@@ -108,13 +101,6 @@ function initializeListeners(key: string, state: NavigationState) {
108101
}
109102

110103
function createNavigationPropConstructor(key: string) {
111-
const actionSubscribers = reduxSubscribers.get(key);
112-
invariant(
113-
actionSubscribers,
114-
"Before calling `createReduxContainer`, please call " +
115-
"`createReactNavigationReduxMiddleware`, so that we know " +
116-
"when to trigger your listener.",
117-
);
118104
return <State: NavigationState>(
119105
dispatch: NavigationDispatch,
120106
state: State,
@@ -139,7 +125,7 @@ function createNavigationPropConstructor(key: string) {
139125
router,
140126
state,
141127
dispatch,
142-
actionSubscribers,
128+
getReduxSubscribers(key),
143129
getScreenProps,
144130
getCurrentNavigation,
145131
);

0 commit comments

Comments
 (0)