Get route params inside actioncreator/selector?

心不动则不痛 提交于 2021-02-11 06:18:16

问题


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 thus the params props has not been injected.

Is there a way around this?


回答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';

<Provider store={store}>
     <ReduxRouter>
            <Route path="/" 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);


来源:https://stackoverflow.com/questions/38069343/get-route-params-inside-actioncreator-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!