Vik*_*tya 7 javascript router reactjs react-router redux
如何使用reducer中的redux-router获取当前路径和查询当前位置.我可以使用mapStateToProps在组件内轻松获取路径名,但我想访问reducer中的当前路径.我正在使用redux-router 1.0.0-beta7,react-router 1.0.3.
Kok*_*lav 16
1 .way - 通过redux-thunk和getState()传递具有特定操作的路径名
const someAction = data =>(dispatch,getState)=>{
dispatch({
type:'SOME_ACTION',
pathname:getState().router.pathname
})
}
Run Code Online (Sandbox Code Playgroud)
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}
Run Code Online (Sandbox Code Playgroud)
3 .way - 从组件的this.props.location.pathname传递路径名
//in component
let {pathname}= this.props.location;
this.props.someAction(data, pathname);
//workflow: component -> action -> reducer
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7203 次 |
| 最近记录: |