如何在reducer中使用redux-router获取当前路径和当前位置的查询。我可以使用mapStateToProps很容易地在组件中获取路径名,但我想访问reducer中的当前路径。我使用redux-router 1.0.0-beta7,react-router 1.0.3。
发布于 2016-06-29 03:26:32
1.way -通过和getState()传递带有特定操作的路径名
const someAction = data =>(dispatch,getState)=>{
dispatch({
type:'SOME_ACTION',
pathname:getState().router.pathname
})
}2.way -编写中间件,并为每个操作传递路径名
///write middleware
export const attachPathNameToAction = store => next => action=>{
action.pathname = store.getState().router.pathname //<-----passing pathname
next(action)
};
///then in reducer you allways can get pathname
case 'SOME_ACTION':
let pathname = action.pathname \\ <-------------
return {...state, action.data}3.way -从组件的this.props.location.pathname传递路径名
//in component
let {pathname}= this.props.location;
this.props.someAction(data, pathname);
//workflow: component -> action -> reducerhttps://stackoverflow.com/questions/38084121
复制相似问题