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

206
Vistas
How to use onClick for two functions

I'm attempting to have two functions run when a button is clicked. Each function works independent of each other, however handleSubmit() does nothing when I write onClick as an arrow function. When I click the button with both functions only editCart() runs. If i attempt this without using an arrow function I get a "too many re-render" error. I've also attempted to create a single wrapper function that calls both functions with no luck. As an aside do I pass the parameters for each function in the onClick arrow function?

  const { register, watch, handleSubmit, setValue } = useForm({
    defaultValues: {
      hoodieQuantity: 1,
    },
  });
  const onSubmit = (data) => {
    console.log(data);
  };

  const hoodieQuantity = watch("hoodieQuantity");
  const increaseQuantity = () =>
    setValue("hoodieQuantity", hoodieQuantity + 1);
  const decreaseQuantity = () => {
    if (hoodieQuantity > 1) {
      setValue("hoodieQuantity", hoodieQuantity - 1);
    }
  };

  const cartArray = [
    {
      name: "Hoodie",
      quantity: "0"
    },
    {
      name: "Sweats",
      quantity: "0"
    },
    {
      name: "Shirt",
      quantity: "0"
    },
    {
      name: "Mousepad",
      quantity: "0"
    },
  ]

  const [currentCart, setCurrentCart] = useState(cartArray);

  const editCart = () => {
    setCurrentCart(currentCart.map((cartItem, hoodieQuantity) =>
      cartItem.name === "Hoodie" ? { ...cartItem, quantity: {hoodieQuantity} } : { ...cartItem }

      )
    
    );
    console.log(cartArray);
  };
  <CounterContainer>
    <CounterWrapper>
      <CounterButtons onClick={decreaseQuantity}>
        -
      </CounterButtons>
    </CounterWrapper>
    <CounterWrapper>
      <CounterInput
        // quantity={quantity}
        {...register("hoodieQuantity")}
        placeholder="HoodieQuantity"
        id="hoodieQuantity"
        type="number"
      >
        {hoodieQuantity}
      </CounterInput>
    </CounterWrapper>
    <CounterWrapper>
      <CounterButtons onClick={increaseQuantity}>
        +
      </CounterButtons>
    </CounterWrapper>
  </CounterContainer>
<Btn onClick={() => [handleSubmit(onSubmit), editCart("Hoodie", hoodieQuantity)] }>Add to Cart</Btn>
about 4 years ago · Juan Pablo Isaza
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