react-navigation-current-route

1.0.0 • Public • Published

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 😎
/* 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:

Package Sidebar

Install

npm i react-navigation-current-route

Weekly Downloads

16

Version

1.0.0

License

ISC

Last publish

Collaborators

  • ayushinigam