8

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.

8
  • Did you have any joy with what I suggested? Commented Jul 1, 2016 at 11:18
  • Thanks for your suggestion but I'm looking for exactly what I asked. If thats not possible then I'll need to find another way. I'm waiting for a little longer and if it's 100% that we can not extract params from the URL then I'll accept your answer. Commented Jul 1, 2016 at 11:20
  • I don't think extracting "the URL params inside a Redux middleware" makes sense. Can you point me to the documentation where you've seen anything like that? Commented Jul 1, 2016 at 11:22
  • I can not point it. Commented Jul 1, 2016 at 11:26
  • Is there any way to map the route params to state? Commented Jul 1, 2016 at 11:27

2 Answers 2

6

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)
};

MORE INFO

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I also ended up looking at redux-router as the closest answer for this. Fortunately, the exact solution for my problem (here: stackoverflow.com/questions/38145321/…) did not require me to change to this router.
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.