使用 React-router 在 Redux 中间件中获取 url 参数
Get url params inside a Redux middleware using React-router
我想提取 Redux 中间件中的 URL 参数,以便分派操作并在有效负载中使用这些值。
您不需要在中间件内部访问 params
。一个常见的模式是通过组件中的 props
访问 params
,您希望从中发送操作。
像这样:
this.props.dispatch(yourAction(this.props.params));
请注意,您需要使用 react-redux Connect method 使 dispatch
可用于您的组件。例如:
export default connect()(YourComponent);
为了实现你的目标,你可以使用redux-router
此库允许您将路由器状态保存在 Redux 存储中。 因此获取当前路径名、查询和参数就像选择应用程序状态的任何其他部分一样简单。
之后你可以从中间件
获取你的参数
export const someMiddleware = store => next => action=> {
/// get params
let params = store.getState().router.params;
///then do what you want...........
next(action)
};
我想提取 Redux 中间件中的 URL 参数,以便分派操作并在有效负载中使用这些值。
您不需要在中间件内部访问 params
。一个常见的模式是通过组件中的 props
访问 params
,您希望从中发送操作。
像这样:
this.props.dispatch(yourAction(this.props.params));
请注意,您需要使用 react-redux Connect method 使 dispatch
可用于您的组件。例如:
export default connect()(YourComponent);
为了实现你的目标,你可以使用redux-router
此库允许您将路由器状态保存在 Redux 存储中。 因此获取当前路径名、查询和参数就像选择应用程序状态的任何其他部分一样简单。
之后你可以从中间件
获取你的参数export const someMiddleware = store => next => action=> {
/// get params
let params = store.getState().router.params;
///then do what you want...........
next(action)
};