Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda