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

77
Visualizações
wrote a React code snippet to implement conditional rendering and am not able to update the state of {Left} and {Right}

The terminal/console returns no error. I am confused as to where I am going wrong. The handleLeftClicks() is used to render the # of times the left button is clicked and respectively for right. The allClicks within History component is meant to render total # of clicks. I am unable to update both {left} and {right}. I tried console logging both the functions but couldn't get an output in the console.

I really apologise if this newbie question wasted your time.

import React, { useState } from "react";

const History = (props) => {
  if (props.allClicks.length === 0) {
    return <div>The app is used by pressing the buttons</div>;
  }

  return <div>button press history: {props.allClicks.join(" ")}</div>;
};

const Button = ({ handleClick, text }) => (
  <button onClick={handleClick}>{text}</button>
);

const App = () => {
  const [left, setLeft] = useState(0);
  const [right, setRight] = useState(0);

  const [allClicks, setAll] = useState([]);

  const handleLeftClicks = () => {
    console.log("left click");

    setAll(allClicks.concat("L"));
    setLeft(left + 1);
  };

  const handleRightClicks = () => {
    console.log("right click");

    setAll(allClicks.concat("R"));
    setRight(right + 1);
  };
  console.log(left);
  console.log(right);
  console.log(allClicks);

  return (
    <div>
      {left}
      <Button handleClick={handleLeftClicks} text="left" />

      <Button handleClick={handleRightClicks} text="right" />

      {right}

      <History allClicks={allClicks} />
    </div>
  );
};

export default App;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are passing a wrong prop to Button component. It should be handleClick instead of onClick.

<Button handleClick={handleLeftClicks} text="left" />

Code Sandbox

about 4 years ago · Juan Pablo Isaza Relatório
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