React - show box after 3 clicks

0 votes
191 views
added May 31, 2023 in React by lcjr First Warrant Officer (11,830 points)
import React, { useState } from 'react';

const ShowBoxComponent = () => {
  const [clickCount, setClickCount] = useState(0);
  const [showBox, setShowBox] = useState(false);

  const handleClick = () => {
    const newClickCount = clickCount + 1;

    if (newClickCount === 3) {
      setShowBox(true);
    }

    setClickCount(newClickCount);
  };

  return (
    <div>
      <button onClick={handleClick}>Click Me</button>
      {showBox && <p className="namasaya">The box shows after 3 clicks</p>}
      <p>Click Count: {clickCount}</p>
    </div>
  );
};

export default ShowBoxComponent;

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...