Answers for "update useState using function"

2

update state in useState hook

// declare state using useState hook.
// someState is set to someInitialState
const [someState, setSomeState] = useState(someInitialState);

// setSomeState updates the current state 
setSomeState(someOtherState);
Posted by: Guest on August-21-2020
0

update useState using function

function DelayedCount() {
  const [count, setCount] = useState(0);

  const handleClickAsync = () => {
    setTimeout(function delay() {
      setCount(count => count + 1);    }, 3000);
  }

  return (
    <div>
      {count}
      <button onClick={handleClickAsync}>Increase async</button>
    </div>
  );
}
Posted by: Guest on July-28-2021
0

react usestate functional update

() => setCount(prevCount => prevCount - 1)
Posted by: Guest on July-20-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language