Home > Enterprise >  how to use setTimeout useState in React with Typescript?
how to use setTimeout useState in React with Typescript?

Time:11-18

I want to have a div, when you click it, the text will change for a few sec. but with ts there is an error.Line 22:7 Expected an assignment or function call and instead saw an expression @typescript-eslint/no-unused-expressions

my Code:

  const [showCopy, setShowCopy] = useState(false);


  const handleClick = () => {
    setShowCopy(true);
    setTimeout(() => {        // <- this is Line 22:7 | error
      setShowCopy(false), 2000;
    });
  };

my JSX:


 <div  onClick={() => {
            navigator.clipboard.writeText("1234");
            handleClick();
          }}
>
            {showCopy ? (
              "you copyed the no."
            ) : (
             "1234"
            )}
/div>

CodePudding user response:

Need to add time as a second argument in setTimeout

setTimeout(() => {        
  setShowCopy(false);
}, 2000); 
  • Related