react-navigation-current-route
v1.0.0
Published
Get current route from react-navigation routes
Downloads
6
Maintainers
Readme
react-navigation-current-route
Get the current route name for an application which is using react-navigation
Installation:
npm install react-navigation-current-route --save
Usage: Pass the
- Get the navigation state, as in redux store ->
navigationState
- Pass
navigationState
to the method exported from react-navigation-current-route - The method returns the current route name :sunglasses:
/* Get current route in a container */
import {connect} from 'react-redux';
import result from 'lodash/result';
import getCurrentRouteName from 'react-navigation-current-route';
...
// Pass the current navigation state object
const currentRoute = getCurrentRouteName(this.props.navigationState)
...
// Get the current navigation state from the redux store
export const mapStateToProps = (state) => ({
navigationState: result(state, 'nav', {}),
});
export default connect(mapStateToProps, null)(/* component-name*/);
/* Get current route in a middleware like saga */
import {call,select} from 'redux-saga/effects';
import result from 'lodash/result';
import getCurrentRouteName from 'react-navigation-current-route';
// Get the current navigation state from the redux store
export const getNavigationState = (state) => result(state, 'nav', null);
export function* someSaga () {
const navigationState = yield select(getNavigationState);
// Pass the current navigation state object
const currentRoute = yield call(getCurrentRouteName, navigationState);
// currentRoute would then have the current route name
}
References: