Installations
npm install react-navigation-sxf-redux-helpers
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
12.12.0
NPM Version
6.11.3
Score
67.1
Supply Chain
97.8
Quality
74.3
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
renjinlong
Download Statistics
Total Downloads
416
Last Day
1
Last Week
3
Last Month
11
Last Year
48
GitHub Statistics
NOASSERTION License
1 Commits
2 Watchers
1 Branches
1 Contributors
Updated on Nov 21, 2019
Package Meta Information
Latest Version
1.0.0
Package Id
react-navigation-sxf-redux-helpers@1.0.0
Unpacked Size
17.94 kB
Size
5.67 kB
File Count
9
NPM Version
6.11.3
Node Version
12.12.0
Total Downloads
Cumulative downloads
Total Downloads
416
Last Day
0%
1
Compared to previous day
Last Week
50%
3
Compared to previous week
Last Month
266.7%
11
Compared to previous month
Last Year
-15.8%
48
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
0 existing vulnerabilities detected
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
Found 0/1 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
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'
Score
3
/10
Last Scanned on 2025-02-03
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 More