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

78
Vistas
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 Respuestas
Responde la pregunta

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 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