简体   繁体   English

使用React-Router在Redux中间件中获取url参数

[英]Get url params inside a Redux middleware using React-router

我想在Redux中间件中提取URL参数,以便调度操作并在有效负载中使用这些值。

In order to achieve your goal , you can use redux-router 为了实现您的目标,您可以使用redux-router

This library allows you to keep your router state inside your Redux store. 该库允许您将路由器状态保留在Redux存储中。 So getting the current pathname, query, and params is as easy as selecting any other part of your application state. 因此,获取当前路径名,查询和参数与选择应用程序状态的任何其他部分一样容易。

after that you can get your params from middleware 之后,您可以从中间件获取参数

export const someMiddleware = store => next => action=> {
    /// get params
    let params = store.getState().router.params;

    ///then do what you want...........
    next(action)
};

MORE INFO 更多信息

You don't need to access params inside the middleware. 您不需要访问中间件内部的params A common pattern is just to access params via props in the component from where you want to dispatch the action. 一种常见的模式是通过组件中的props访问params ,您可以从中分配操作。

Something like this: 像这样:

this.props.dispatch(yourAction(this.props.params));

Note that you need to make dispatch available to your component by using react-redux Connect method . 请注意,您需要使用react-redux Connect方法使dispatch对您的组件可用。 For example: 例如:

export default connect()(YourComponent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM