callback on usestate code example
Example 1: react callback set staet
setState( { name: "Michael" }, () => console.log(this.state));
Example 2: callback in react
import React, { useCallback } from 'react';
function MyComponent() {
const handleClick = useCallback(() => { console.log('Clicked!'); }, []);
}
Example 3: usestate hook callback
const [counter, setCounter] = useState(0);
const doSomething = () => {
setCounter(123);
}
useEffect(() => {
console.log('Do something after counter has changed', counter);
}, [counter]);