0

I am making an "Add to Cart" Functionality. My code is as follows :

import React, { Component } from 'react';
import Items from '../json/items.json';

class AddToCart extends Component {

    putInCart(i) {
        // **** I WANT TO GET THE QUANTITY IN THIS FUNCTION ****
    }

    render() {

        return (
            <div>
                {
                    Items.items.map((val, ind) => {
                        return (
                            <div>
                                <p>Name : {val.name}</p>
                                <p>Price : {val.price}</p>
                                <p>Availability : {val.availability}</p>
                                <input placeholder="Quantity"></input>
                                <br />
                                <button onClick={() => this.putInCart(ind)}>Add to Cart</button>
                                <br /><br /><br />
                            </div>
                        );
                    })
                }
            </div>
        );
    }
}

export default AddToCart;

I want to get the quantity which the user types in the quantity text field in my "putInCart()" method when the user clicks the "add to cart" button. But I can't figure out how to do that. Any help would be appreciated.

JSON File :-

    {
     "items": [
       {
        "name": "Item 1",
        "price": "100",
        "availability": "In Stock",
       },
       {
        "name": "Item 2",
        "price": "50",
        "availability": "Out of Stock",
      },
      {
        "name": "Item 3",
        "price": "150",
        "availability": "In Stock",
      }
    ]
   }
1

1 Answer 1

1

You need to store the quantity written by the user in a property of the state of the component,

  1. Create a state with an initial empty string
  2. Bind the input value to this state property
  3. On every value change, you update the state value
  4. Now you can get the state value everywhere you want in the component

src/App.js

import React, { Component } from "react";
import "./styles.css";
import Items from './items';

class AddToCart extends Component {
   constructor(props) {
     super(props);
     this.state = {
       1: "",
       2: "",
       3: ""
     };
     this.handleChange = this.handleChange.bind(this);
   }

    handleChange(e) {
      this.setState({ [e.target.name]: e.target.value });
    }


    putInCart(i) {
      console.log(this.state[i]);
    }

    render() {
        return (
            <div>
                {
                    Items.map((val, ind) => {
                        return (
                            <div key={ind}>
                                <p>Name : {val.name}</p>
                                <p>Price : {val.price}</p>
                                <p>Availability : {val.availability}</p>
                                <input placeholder="Quantity" name={ind} onChange={this.handleChange} value={this.state.inputValue}></input>
                                <br />
                                <button onClick={() => this.putInCart(ind)}>Add to Cart</button>
                                <br /><br /><br />
                            </div>
                        );
                    })
                }
            </div>
        );
    }
}

export default AddToCart;

src/items.js

export default [
    {
      "name": "Item 1",
      "price": "100",
      "availability": "In Stock",
    },
    {
      "name": "Item 2",
      "price": "50",
      "availability": "Out of Stock",
    },
    {
      "name": "Item 3",
      "price": "150",
      "availability": "In Stock",
    }
]
Sign up to request clarification or add additional context in comments.

12 Comments

With this solution you can't have multiple values in items. All the input will be bind to the same value
I did some edits, but in the val of Items.items , is there a unique property to differentiate it from others ? For now I use the "ind" to put it as the name of input, and then in the this.setState I use e.target.name
ok and in putInCart function, which input value do you need to get
The <input> tag value inside the map function.
Ok I made some final changes, it can be ok for now, tell me if sometihng is not wrong
|

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.