如何访问减速器(redux-router)中的当前位置? [英] How to access current location in reducer (redux-router)?

查看:57
本文介绍了如何访问减速器(redux-router)中的当前位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 reducer 中使用 redux-router 获取当前路径和查询当前位置.我可以使用 mapStateToProps 在组件内轻松获取路径名,但我想访问 reducer 中的当前路径.我使用的是 redux-router 1.0.0-beta7,react-router 1.0.3.

How would i get the current path and query of the current location with redux-router in reducer. I am able to get the pathname easily inside the component with mapStateToProps, but i want to access the current path in reducer. I am using redux-router 1.0.0-beta7, react-router 1.0.3.

推荐答案

1.way - 将 pathname 和特定的 action 传递到 >redux-thunk 和 getState()

1.way - pass pathname with particular action through redux-thunk and getState()

const someAction = data =>(dispatch,getState)=>{
   dispatch({
      type:'SOME_ACTION',
      pathname:getState().router.pathname
   })
}

2.way - 编写中间件并在每个动作中传递路径名

2.way - write middleware and pass pathname with every action

///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 传递路径名

3.way - pass pathname from component's this.props.location.pathname

//in component 
let {pathname}= this.props.location;
this.props.someAction(data, pathname);

//workflow:  component -> action -> reducer

这篇关于如何访问减速器(redux-router)中的当前位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆