I'd like to extract the URL params inside a Redux middleware in order to dispatch an action and use those values in the payload.
Get url params inside a Redux middleware using React-router
Asked Answered
In order to achieve your goal , you can use redux-router
This library allows you to keep your router state inside your Redux store. 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)
};
Thanks, I also ended up looking at redux-router as the closest answer for this. Fortunately, the exact solution for my problem (here: #38145821) did not require me to change to this router. –
Seger
You don't need to access params
inside the middleware. A common pattern is just to access params
via props
in the component from where you want to dispatch the action.
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. For example:
export default connect()(YourComponent);
© 2022 - 2024 — McMap. All rights reserved.
array.map
? – Sibert