2

I'm fairly new to React development. In the shopping application that I'm developing, I'm trying to send a post request through a button to my rest services to add a product to the cart. I've added the axios library to perform this. Since all the online tutorials get the info to the requests through forms I was wondering how do I collect the information I got from an earlier get request and send it back as a post request to another data table. The following is my approach which does not work.

 <button onClick={this.add.bind(this,this.state.groups.map(group => <div key={group.id}>{group.name}</div> ))} className="flex-c-m size1 bg4 bo-rad-23 hov1 s-text1 trans-0-4"> </button> 

the post request config method looks something like this

add() { axios({ method: 'post', url: '/api/cart', body: { productid: '', productname: '' } }); } 

Please help me in figuring this out. Thanks in advance.

5
  • What is this.state.groups? Are you trying to render buttons in loop based on no of groups? Commented Feb 23, 2019 at 7:00
  • Groups is the array in which the earlier get request is stored in Commented Feb 23, 2019 at 7:02
  • You didn’t answer my question what are you trying to achieve with following code ? {this.add.bind(this,this.state.groups.map(group => <div key={group.id}>{group.name}</div> ))} Commented Feb 23, 2019 at 7:07
  • I’m trying to collect the productid and productname of the get request from my product table which I got earlier and send it to the cart table as a post request. It’s an “Add to Cart” button Commented Feb 23, 2019 at 7:10
  • Please check my updated answer Commented Feb 23, 2019 at 8:04

3 Answers 3

1

I think you are trying to render set of buttons in loop based of no of groups

  1. Render button in loop
  2. Pass group id and name to add function as parameters
  3. Never do function binding directly in render, instead do always in constructor

Bind it manually in constructor

 constructor(props){ super(props){ this.add = this.add.bind(this); } } 

Iterate groups and render buttons like below

{this.state.groups.map(group => <button key={group.id} onClick={() => this.add(group.id, group.name)} className="flex-c-m size1 bg4 bo-rad-23 hov1 s-text1 trans-0-4">Add to Cart</button> )} 

Now your add function should look like below

add(id, name) { axios({ method: 'post', url: '/api/cart', body: { productid: id, productname: name } }); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer Hemadri Dasari.
0

This should work .

Edit oomyk5m64q

Comments

0

This is my rest server implementation

@PostMapping("/cart") public Cart create(String productName, @RequestBody Map<String, String> body){ String productid = body.get("productid"); String productname=("productname"); return cartRepository.save(new Cart(productid,productname)); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.