Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

76
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!