Skip to content

Commit 7c1b36d

Browse files
authored
Merge pull request react-navigation#193 from Ashoat/source
Update Redux Integration docs for react-navigation-redux-helpers@2.0.0
2 parents 99d8c1c + 8fec24f commit 7c1b36d

File tree

1 file changed

+9
-30
lines changed

1 file changed

+9
-30
lines changed

docs/redux-integration.md

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ If your only reason for doing this is that you want to be able to perform naviga
1212

1313
## Overview
1414

15-
1. To handle your app's navigation state in Redux, you can pass your own [`navigation`](navigation-prop.html) prop to a navigator. This `navigation` prop is an object that must contain the navigation [`state`](navigation-prop.html#state-the-screen-s-current-state-route), [`dispatch`](navigation-prop.html#dispatch-send-an-action-to-the-router), and `addListener` properties.
15+
1. To handle your app's navigation state in Redux, you can pass your own [`navigation`](navigation-prop.html) prop to a navigator. `react-navigation-redux-helpers` handles this for you behind the scenes with a "higher-order component" called `reduxifyNavigator`. You pass in your root navigator component to the `reduxifyNavigator` function, and it returns a new component that takes your navigation `state` and `dispatch` function as props.
1616

17-
2. The aforementioned navigation `state` will need to be kept updated using React Navigation's navigation reducer. You will call this reducer from your Redux master reducer.
17+
2. A middleware is needed so that any events that mutate the navigation state properly trigger React Navigation's event listeners.
1818

19-
3. React Navigation's `dispatch` will just be Redux's default `dispatch`, which you pass in to React Navigation as part of the `navigation` prop. As such, you will be able to dispatch normal Redux actions using `this.props.navigation.dispatch(ACTION)`.
20-
21-
4. A middleware is needed so that any events that mutate the navigation state properly trigger the event listeners. To properly trigger the event listeners with the initial state, a call to `initializeListeners` is also necessary.
19+
3. The navigation state inside Redux will need to be kept updated using React Navigation's navigation reducer. You will call this reducer from your Redux master reducer.
2220

2321
## Step-by-step guide
2422

@@ -48,10 +46,9 @@ import {
4846
combineReducers,
4947
} from 'redux';
5048
import {
51-
createNavigationPropConstructor, // handles #1 above
52-
createNavigationReducer, // handles #2 above
53-
createReactNavigationReduxMiddleware, // handles #4 above
54-
initializeListeners, // handles #4 above
49+
reduxifyNavigator,
50+
createReactNavigationReduxMiddleware,
51+
createNavigationReducer,
5552
} from 'react-navigation-redux-helpers';
5653
import { Provider, connect } from 'react-redux';
5754
import React from 'react';
@@ -64,34 +61,16 @@ const appReducer = combineReducers({
6461
...
6562
});
6663

67-
// Note: createReactNavigationReduxMiddleware must be run before createNavigationPropConstructor
64+
// Note: createReactNavigationReduxMiddleware must be run before reduxifyNavigator
6865
const middleware = createReactNavigationReduxMiddleware(
6966
"root",
7067
state => state.nav,
7168
);
72-
const navigationPropConstructor = createNavigationPropConstructor("root");
73-
74-
class App extends React.Component {
75-
componentDidMount() {
76-
initializeListeners("root", this.props.nav);
77-
}
78-
79-
render() {
80-
this._navigation = navigationPropConstructor(
81-
this.props.dispatch,
82-
this.props.nav,
83-
AppNavigator.router,
84-
() => this._navigation
85-
);
86-
return <AppNavigator navigation={this._navigation} />;
87-
}
88-
89-
}
9069

70+
const App = reduxifyNavigator(AppNavigator, "root");
9171
const mapStateToProps = (state) => ({
92-
nav: state.nav,
72+
state: state.nav,
9373
});
94-
9574
const AppWithNavigationState = connect(mapStateToProps)(App);
9675

9776
const store = createStore(

0 commit comments

Comments
 (0)