This package allows the user to manage their React Navigation state from within Redux.
React Navigation no longer supports storing its state within Redux. You should be able to refactor your custom navigation logic into custom navigators, custom routers, and onStateChange
callbacks.
Most projects that are using both Redux and React Navigation don't need this library. Things like state persistence and BackHandler
behavior aren't handled directly by createReduxContainer
, but are handled by the default createAppContainer
. However, there are some things this library makes easier:
isTransitioning
to true!) yarn add react-navigation-redux-helpers
or
npm install --save react-navigation-redux-helpers
import {
createStackNavigator,
} from 'react-navigation';
import {
createStore,
applyMiddleware,
combineReducers,
} from 'redux';
import {
createReduxContainer,
createReactNavigationReduxMiddleware,
createNavigationReducer,
} from 'react-navigation-redux-helpers';
import { Provider, connect } from 'react-redux';
import React from 'react';
const AppNavigator = createStackNavigator(AppRouteConfigs);
const navReducer = createNavigationReducer(AppNavigator);
const appReducer = combineReducers({
nav: navReducer,
...
});
const middleware = createReactNavigationReduxMiddleware(
state => state.nav,
);
const App = createReduxContainer(AppNavigator);
const mapStateToProps = (state) => ({
state: state.nav,
});
const AppWithNavigationState = connect(mapStateToProps)(App);
const store = createStore(
appReducer,
applyMiddleware(middleware),
);
class Root extends React.Component {
render() {
return (
<Provider store={store}>
<AppWithNavigationState />
</Provider>
);
}
}
createReactNavigationReduxMiddleware
(required)function createReactNavigationReduxMiddleware<State: {}>(
navStateSelector: (state: State) => NavigationState,
key?: string,
): Middleware<State, *, *>;
navStateSelector
selects the navigation state from your store.key
needs to be unique for the Redux store and consistent with the call to createReduxContainer
below. You can leave it out if you only have one store.createReduxContainer
(required)function createReduxContainer(
navigator: Navigator,
key?: string,
): React.ComponentType<{ state: NavigationState, dispatch: Dispatch }>;
navigator
is your root navigator (React component).key
needs to be consistent with the call to createReactNavigationReduxMiddleware
above. You can leave it out if you only have one store.state
and dispatch
props that you get via react-redux
's connect
.createNavigationReducer
(optional)function createNavigationReducer(navigator: Navigator): Reducer<*, *>;
createNavigationReducer
in the global scope to construct a navigation reducer.navigator.router.getStateForAction
, which you can call directly if you'd prefer.navigator
is your root navigator (React component).combineReducers
.To make Jest tests work with your React Navigation app, you need to change the Jest preset in your package.json
:
"jest": {
"preset": "react-native",
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?react-native|@?react-navigation|react-navigation-redux-helpers)"
]
}
Here is a code snippet that demonstrates how the user might handle the hardware back button on platforms like Android:
import React from "react";
import { BackHandler } from "react-native";
import { NavigationActions } from "react-navigation";
/* your other setup code here! this is not a runnable snippet */
class ReduxNavigation extends React.Component {
componentDidMount() {
BackHandler.addEventListener("hardwareBackPress", this.onBackPress);
}
componentWillUnmount() {
BackHandler.removeEventListener("hardwareBackPress", this.onBackPress);
}
onBackPress = () => {
const { dispatch, nav } = this.props;
if (nav.index === 0) {
return false;
}
dispatch(NavigationActions.back());
return true;
};
render() {
/* more setup code here! this is not a runnable snippet */
return <AppNavigator navigation={navigation} />;
}
}