Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
How can I increase and decrease the quantity in reactjs?

I have a button and an input form. When clicking the button, my product quantity decreases by 1 and increases quantity when submitting the form. Quantity increases with the form value. How can I complete this problem in Reactjs?

import React, { useEffect, useState } from "react";

const InventoryDetails = () => {
  const [inventory, setInventory] = useState({});

  useEffect(() => {
    const url = `inventory.json`;

    fetch(url)
      .then((res) => res.json())
      .then((data) => setInventory(data));
  }, []);

  const handleDelivered = () => {
    // can't understand what can I do here.
  };
  const restockItem = () => {};

  return (
    <div>
      <div>
        <h6> Quantity: {inventory.quantity} </h6>

        <button onClick={handleDelivered}>Delivered</button>
      </div>

      <div>
        <h3>Restock the items</h3>
        <form onSubmit={restockItem}>
          <input
            type="number"
            name="number"
            id=""
            placeholder="Enter quantity"
            required
          />

          <input className="" type="submit" value="Restock" />
        </form>
      </div>
    </div>
  );
};
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Here I wrote two functions handleDelivered and restockItem. Put restockItem function to your button and try them:

handleDelivered = () => {
    setInventory({
        ...inventory,
        quantity: inventory.quantity - 1,
    });
};

restockItem = (event) => {
    event.preventDefault();

    setInventory({
        ...inventory,
        quantity: inventory.quantity + parseInt(event.target[0].value),
    });
};

You have to set the state of the inventory when you're going to make changes, ...inventory is destructing the inventory object to get all the fields inside and change only quantity field, preventDefault is just my habit to prevent submitting, don't forget to type check the quantity field.

about 4 years ago · Juan Pablo Isaza Report

0

You can use these tricks to solve this problem -

  1. When you add or remove quantity from your stocks you can also update the database also using a 'PUT' method and set a dependency on your use effect over product. So when the product data updated in the database its also updated in the UI because of the dependency.
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!