Step2. Hence we know that the useState function will return an array including two values. The first value is the target state and second one is the corresponding setState function.
Step4. Nevertheless, we can execute the useState several times in one application. So how to reach this? The answer is that hooks are not magic but only arrays.
Step2. As we saw above, useEffect function has a callback parameter and this callback will be executed.
functionuseEffect(callback){callback();}
Step3. The useEffect function can have a second parameter which should be an array standing for the dependencies. Meanwhile, we should also consider the multiple times to execute useEffect.
leteffectQueue=[];letindex=0;functionuseEffect(callback,deps){if(Array.isArray(deps)){return'The second parameter should be array.';}elseif(!deps)){callback();}else{constprevDeps=effectQueue[index];consthasChanged=deps.every((dep,index)=>{return!(dep===prevDeps[index]);});if(hasChanged){callback();}effectQueue[index]=deps;}index++;}