0

I have a URL as below with various querystring values.

http://localhost:3000/product/?id=123&name=test

I am working on isomorphic application using redux, react. So i am firing action and may i know how can i get these query string values to redux action file?

export function getProductData(params) {

  debug('Action Requested:' + params);  

  return {
    type:    GET_PRODUCT_DATA,
    promise: request.get(API_URL + params.productId)
  }
};

Because in action i am firing the API call and i need that ID from querystring value.

3
  • If You need to use response in action then dispatch in ajax callback not do promise in action. But I don't know if this is what You want to do. Commented Oct 8, 2016 at 15:46
  • This is isomorphic application, using this github.com/bananaoomarang/isomorphic-redux/tree/tutorial Commented Oct 8, 2016 at 15:48
  • My question is how to access the querystring values? if the url is like localhost:3000/product/1234, it is working as we code in the router like /product/:productid Commented Oct 8, 2016 at 15:49

2 Answers 2

2

If you're using React-router, go ahead and access them via props.location.query

Would ultimately looks something like:

const Product = (props) =>
  <div>
     <button onClick={e => props.getProductData(props.location.query)}>Do Some Action</button>
  </div>;
Sign up to request clarification or add additional context in comments.

Comments

0

The parameters from the URL according to your routes are propagate to the component using props.params so if you have routes like

<Route path="/product/:productid" component={Product} />

you can access productid from Product Component using props and pass it to your action:

const Product = (props) =>
  <div>
     <button onClick={e => props.getProductData(props.params)}>Do Some Action</button>
  </div>;

where props.params holds all the parameters you define in the route. in our case value of productid will be in props.params.productid.

2 Comments

are querystring also going to part of that? if you have just /product/ and the URL comes as /product/?id=1234
This answer doesn't address the question. Does not mention usage of the query string at all.

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.