this is an experimental code to represent probable misunderstanding of how react works https://jsfiddle.net/Dr_Coder/h6da2jku/78/
const App = () => {
const {
useState,
useEffect
} = React;
const [firstCount, setFirstCount] = useState(0);
const [secondCount, setSecondCount] = useState(0)
const increment = () => setFirstCount(firstCount + 1)
useEffect(() => {
if(firstCount >= 1) setSecondCount(firstCount)
const timer = setTimeout(() => {
setFirstCount(0)
}, 1000);
return () => clearTimeout(timer)
}, [firstCount, secondCount]);
return (<div className = "app" >
<button onClick={increment}>click {firstCount}</button>
<p>{secondCount}</p>
< /div>
)
}
ReactDOM.render( < App / > , document.getElementById('root'))
<div id="root">
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.12.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.12.0/umd/react-dom.production.min.js"></script>
in my mind i think this happens:
1 - clicking the button and this will increment firstCount
2 - rendering with the new value of firstCount
3 - useEffect will work because there is a change in firstCount
dependency value
4 - the condition is true and this will change the value of secondCount
to be equal to the value of firstCount
5 - this will render component again with the new value of secondCount
6 - timer finishes, and set firstCount
to 0, this will render component again
7 - after render useEffect
will work, the condition is false, nothing happens
Is this what really happening in the code, the life cycle of a react component is overwhelming, and i hope someone clarify what is exactly happening in the code!