Answers for "How to use `setState` callback on react hooks"

1

How to use `setState` callback on react hooks

//You need to use useEffect hook to achieve this.

const [counter, setCounter] = useState(0);

const doSomething = () => {
  setCounter(123);
}

useEffect(() => {
   console.log('Do something after counter has changed', counter);
}, [counter]);
Posted by: Guest on May-26-2021
1

How to use `setState` callback on react hooks

//You need to use useEffect hook to achieve this.

const [counter, setCounter] = useState(0);

const doSomething = () => {
  setCounter(123);
}

useEffect(() => {
   console.log('Do something after counter has changed', counter);
}, [counter]);
Posted by: Guest on May-26-2021

Code answers related to "How to use `setState` callback on react hooks"

Code answers related to "Javascript"

Browse Popular Code Answers by Language