I really want to understand the lifecycle of react functional component. In many websites you see these three steps:
1-mounthing 2-render 3-unmounthing.
But what about other code that is written before useeffect() function for example assume this:
const Countdown = () => {
let x = 0;
const [countDown, setCountDown] = useState(10)
x = x + 1
if(x > 100) {
x = 0
}
useEffect(() => {
const interval = setInterval(() => {
setCountDown(countDown - 1);
console.log(x)
}, 1000);
}, [countDown]);
};
I want to know :
when countDown state and x variable is declared before useEffect or after it (or inside it)?
when
iforforphrases is declared(in this exampleifphrase), Are they inside useEffect indeed?
what is loading page order? what is start point of executing?
The lifecycle
It's more like (docs):
useLayoutEffect)useEffect))useLayoutEffect)useEffect))Your Questions
Before. The React library can't change how JavaScript code execution occurs. The
useStatecall and associated declarations are before theuseEffectcall, so they happen before it.No, only the code within the
useEffectcallback is called as an effect.How your code runs
The cycle here is:
xvariable and sets it to0.countDownandsetCountDownand callsuseState, which allocates a state slot in the instance storage; your code stores whatuseStatereturns (the initial state value and the setter) in those constants.x = x + 1statement runs, updatingxto1.ifstatement runs, but the condition will never be true —xis a local variable, not a state member, so its value will always be1at this point.useEffectschedules an effect callback for whencountDownchanges.Countdown.Countdownshould return and commits them to the DOM (making the DOM show what they describe).useEffectcallback (useEffectcallbacks are always called just after mount)setCountDown.x, which will be1.setCountDown, changing the value.xvariable and sets it to0.countDownandsetCountDownand callsuseState, which retrieves the updated state from the instance storage; your code stores whatuseStatereturns (the current state value and the setter) in those constants.x = x + 1statement runs, updatingxto1.ifstatement runs, but the condition will never be true.useEffectschedules an effect callback for whencountDownchanges.Countdown.countDownchanged, React calls youruseEffectcallbacksetCountDown.x, which will be1.Issues with the code
There are a couple of bugs in the code you've shown
countDownchanges. This will quickly lead to hundreds and then thousands of timers all triggering update calls. You should:countDownas a dependency, so the effect only runs on mount. Then use the callback form ofsetCountDown.xto be maintained between calls to the function, but it's a local variable, so it's re-created each time.countDownreaches0, so it will just keep going to-1,-2, etc.Updated version
Here's an updated version with some notes. I was going to remove
xbecause it wasn't really used for anything, but then thought it might be better to leave it with comments. And I didn't do anything about #4 above, because I wasn't sure what you wanted to do.If we wanted the countdown to stop when it reached
0and turn off the timer, there are a couple of ways we might do that, see comments in the two live examples showing different ways: