Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

140
Vistas
How to reset values of a state in React JS

I am learning React JS. I am doing a demo project where I have a discard button to reset the values of state. I have implemented the function and it seems ok to me but not working.

I am passing handleResetCart function through <Cart /> component.

App.js

function App() {
  const [totalPrice, setTotalPrice] = useState([]);
  const [totalItems, setTotalItems] = useState([]);

  const handleCart = course => {
    const newItems = [...totalItems, course];
    setTotalItems(newItems);
    const newPrice = [...totalPrice, course.price];
    setTotalPrice(newPrice);
  };

  const handleResetCart = () => {
    setTotalPrice([]);
    setTotalItems([]);
  }

  return (
    <div className="app">
      <Navbar />
      <div className="container">
        <div className="row">
          <div className="courses col-9">
            {data.map(course => (
              <Course key={course.id} course={course} handleCart={handleCart} />
            ))}
          </div>
          <div className="cart col-3">
            <Cart
              totalPrice={totalPrice}
              totalItems={totalItems}
              handleResetCart={handleResetCart}
            />
          </div>
        </div>
      </div>
    </div>
  );
}

I am calling the handleCarReset function through the Discard button.

Cart.jsx

import React from 'react';

const Cart = props => {
  const { totalPrice } = props;
  const { totalItems } = props;
  const price = totalPrice.reduce((t1, t2) => t1 + t2, 0).toFixed(2);

  return (
    <div className="card">
      <div className="card-body">
        <h5 className="card-title text-center">
          CART <i className="bi bi-cart-plus-fill"></i>
        </h5>
        <hr />
        <p>Courses Added: {totalItems.length}</p>
        <p>Total Price: ${price}</p>
        <hr />
        <div className="d-flex justify-content-between">
          <button
            className="btn btn-outline-danger btn-sm"
            onClick={() => props.handleResetCart}>
            Discard
          </button>
          <button className="btn btn-outline-success btn-sm">Buy Now</button>
        </div>
      </div>
    </div>
  );
};

export default Cart;

Where is the problem? How to solve this?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Seems like in your cart component, you are returning a reference of handleResetCard function when onClick event happens. You need to invoke handleResetCard function instead.

Fix button onClick like below

// cart.jsx

<button
  className="btn btn-outline-danger btn-sm"
  onClick={() => props.handleResetCart()}>
  Discard
</button>

or more concisely without creating an arrow funciton

// cart.jsx

<button
  className="btn btn-outline-danger btn-sm"
  onClick={props.handleResetCart}>
  Discard
</button>
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use it

 onClick={() => props.handleResetCart()}>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda