问题
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