如何访问减速器(redux-router)中的当前位置?
How to access current location in reducer (redux-router)?
我如何使用reducer中的redux-router获取当前路径和当前位置的查询。我可以使用 mapStateToProps 在组件内部轻松获取路径名,但我想访问 reducer 中的当前路径。我正在使用 redux-router 1.0.0-beta7, react-router 1.0.3.
1.way - 通过 传递 pathname 和特定的 action redux-thunk 和 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 -> reducer
我如何使用reducer中的redux-router获取当前路径和当前位置的查询。我可以使用 mapStateToProps 在组件内部轻松获取路径名,但我想访问 reducer 中的当前路径。我正在使用 redux-router 1.0.0-beta7, react-router 1.0.3.
1.way - 通过 传递 pathname 和特定的 action redux-thunk 和 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 -> reducer