Skip to content Skip to sidebar Skip to footer

Get Route Params Inside Actioncreator/selector?

Using redux router, In my components I have access to the params object with information about by URL. However, inside my actioncreators, I get the state through getState(), and th

Solution 1:

to get access params in action creators, you should pass it:

in component

this.props.someAction(this.props.params)

in action creator

const someAction = (params)=>{
    let {data} = params;
    ....
}

to get access params in selector, you should pass it:

in component

const mapStateToProps=(state, ownProps)=({
  data:someSelector(state,ownProps.params)
})

in selector

const someSelector=(state, params)={
   ..some computing
   return result
}

to get access from getState() to router's params, pathname and query:

you need to install redux-router

if you allready using it , then check

1.routes

import { ReduxRouter } from'redux-router';

<Providerstore={store}><ReduxRouter><Routepath="/"component={App}>
         //...routes
            </Route></ReduxRouter></Provider>

2.reducer

import { routerStateReducer } from'redux-router';
const reducer = combineReducers({
router: routerStateReducer,
...
});

3.store enchancer

import { reduxReactRouter} from'redux-router';
import { createHistory } from'history';
import { compose, createStore, applyMiddleware } from'redux';
const store = compose(
  applyMiddleware(m1, m2, ...),
  reduxReactRouter({
    createHistory
  })
)(createStore)(reducer);

Post a Comment for "Get Route Params Inside Actioncreator/selector?"