Installations
npm install react-navigation-redux-helpers
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
10.16.3
NPM Version
6.11.3
Score
74.2
Supply Chain
98.5
Quality
75.4
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
react-navigation
Download Statistics
Total Downloads
4,231,295
Last Day
52
Last Week
1,991
Last Month
8,375
Last Year
145,880
GitHub Statistics
NOASSERTION License
296 Stars
93 Commits
43 Forks
26 Watchers
3 Branches
32 Contributors
Updated on Nov 08, 2024
Package Meta Information
Latest Version
4.0.1
Package Id
react-navigation-redux-helpers@4.0.1
Unpacked Size
17.88 kB
Size
5.66 kB
File Count
9
NPM Version
6.11.3
Node Version
10.16.3
Total Downloads
Cumulative downloads
Total Downloads
4,231,295
Last Day
-84.8%
52
Compared to previous day
Last Week
-35%
1,991
Compared to previous week
Last Month
62.7%
8,375
Compared to previous month
Last Year
-49.1%
145,880
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
react-navigation-redux-helpers
This package allows the user to manage their React Navigation state from within Redux.
How it works
- In React Navigation, "containers" wrap navigators, and own the state for that navigator and any nested navigators. This package implements a container that uses Redux as a backing store.
- A Redux middleware is used so that any events that mutate the navigation state properly trigger React Navigation's event listeners.
- Finally, a reducer enables React Navigation actions to mutate the Redux state.
Motivation
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:
- It's possible to implement custom actions, allowing you to manipulate the navigation state in ways that aren't possible with the stock React Navigation actions. Though it's possible to implement custom routers in React Navigation to do this, it's arguably cleaner via Redux. (If you want animations to run on your action, make sure to set
isTransitioning
to true!) - This library allows the user to customize the persistence of their navigation state. For instance, you could choose to persist your navigation state in encrypted storage. Most users don't need this, as there are no practical downsides to handling persistence of navigation state and Redux state separately. Note that stock React Navigation supports some basic degree of persistence customization.
- You can implement custom reducer behavior to validate state and maintain consistency between navigation state and other application state. This is again possible with custom routers, but likely cleaner to implement without, especially in the context of an existing Redux setup.
Installation
1yarn add react-navigation-redux-helpers
or
1npm install --save react-navigation-redux-helpers
Example
1import { 2 createStackNavigator, 3} from 'react-navigation'; 4import { 5 createStore, 6 applyMiddleware, 7 combineReducers, 8} from 'redux'; 9import { 10 createReduxContainer, 11 createReactNavigationReduxMiddleware, 12 createNavigationReducer, 13} from 'react-navigation-redux-helpers'; 14import { Provider, connect } from 'react-redux'; 15import React from 'react'; 16 17const AppNavigator = createStackNavigator(AppRouteConfigs); 18 19const navReducer = createNavigationReducer(AppNavigator); 20const appReducer = combineReducers({ 21 nav: navReducer, 22 ... 23}); 24 25const middleware = createReactNavigationReduxMiddleware( 26 state => state.nav, 27); 28 29const App = createReduxContainer(AppNavigator); 30const mapStateToProps = (state) => ({ 31 state: state.nav, 32}); 33const AppWithNavigationState = connect(mapStateToProps)(App); 34 35const store = createStore( 36 appReducer, 37 applyMiddleware(middleware), 38); 39 40class Root extends React.Component { 41 render() { 42 return ( 43 <Provider store={store}> 44 <AppWithNavigationState /> 45 </Provider> 46 ); 47 } 48}
API
createReactNavigationReduxMiddleware
(required)
1function createReactNavigationReduxMiddleware<State: {}>( 2 navStateSelector: (state: State) => NavigationState, 3 key?: string, 4): Middleware<State, *, *>;
- Returns a middleware that can be applied to a Redux store.
- Param
navStateSelector
selects the navigation state from your store. - Param
key
needs to be unique for the Redux store and consistent with the call tocreateReduxContainer
below. You can leave it out if you only have one store.
createReduxContainer
(required)
1function createReduxContainer(
2 navigator: Navigator,
3 key?: string,
4): React.ComponentType<{ state: NavigationState, dispatch: Dispatch }>;
- Returns a HOC (higher-order component) that wraps your root navigator.
- Param
navigator
is your root navigator (React component). - Param
key
needs to be consistent with the call tocreateReactNavigationReduxMiddleware
above. You can leave it out if you only have one store. - Returns a component to use in place of your root navigator. Pass it
state
anddispatch
props that you get viareact-redux
'sconnect
.
createNavigationReducer
(optional)
1function createNavigationReducer(navigator: Navigator): Reducer<*, *>;
- Call
createNavigationReducer
in the global scope to construct a navigation reducer. - This basically just wraps
navigator.router.getStateForAction
, which you can call directly if you'd prefer. - Param
navigator
is your root navigator (React component). - Call this reducer from your master reducer, or combine using
combineReducers
.
Miscellaneous
Mocking tests
To make Jest tests work with your React Navigation app, you need to change the Jest preset in your package.json
:
1"jest": { 2 "preset": "react-native", 3 "transformIgnorePatterns": [ 4 "node_modules/(?!(jest-)?react-native|@?react-navigation|react-navigation-redux-helpers)" 5 ] 6}
Back button
Here is a code snippet that demonstrates how the user might handle the hardware back button on platforms like Android:
1import React from "react"; 2import { BackHandler } from "react-native"; 3import { NavigationActions } from "react-navigation"; 4 5/* your other setup code here! this is not a runnable snippet */ 6 7class ReduxNavigation extends React.Component { 8 componentDidMount() { 9 BackHandler.addEventListener("hardwareBackPress", this.onBackPress); 10 } 11 12 componentWillUnmount() { 13 BackHandler.removeEventListener("hardwareBackPress", this.onBackPress); 14 } 15 16 onBackPress = () => { 17 const { dispatch, nav } = this.props; 18 if (nav.index === 0) { 19 return false; 20 } 21 22 dispatch(NavigationActions.back()); 23 return true; 24 }; 25 26 render() { 27 /* more setup code here! this is not a runnable snippet */ 28 return <AppNavigator navigation={navigation} />; 29 } 30}

No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Warn: project license file does not contain an FSF or OSI license.
Reason
5 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g
- Warn: Project is vulnerable to: GHSA-662x-fhqg-9p8v
- Warn: Project is vulnerable to: GHSA-394c-5j6w-4xmx
- Warn: Project is vulnerable to: GHSA-78cj-fxph-m83p
- Warn: Project is vulnerable to: GHSA-fhg7-m89q-25r3
Reason
Found 2/30 approved changesets -- score normalized to 0
Reason
project is archived
Details
- Warn: Repository is archived.
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
- Warn: branch protection not enabled for branch '3.x'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 2 are checked with a SAST tool
Score
2.3
/10
Last Scanned on 2025-02-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to react-navigation-redux-helpers
react-native-navigation-redux-helpers
Redux helpers for React Native navigation
react-navigation-sxf-redux-helpers
Redux middleware and utils for React Navigation
@meth/react-navigation-redux-helpers
Redux middleware and utils for React Navigation
react-native-navigation-redux-helpers-compat
Redux helpers for React Native navigation experimental compat