setstate not immediately updating dom code example
Example 1: react state not updating immediately
//In case of hooks, you should use useEffect hook.
const [fruit, setFruit] = useState('');
setFruit('Apple');
useEffect(() => {
console.log('Fruit', fruit);
}, [fruit])
Example 2: setstate not updating state immediately
this.setState({pencil:!this.state.pencil}, myFunction)