all! I'm new to React. Currently, I meet a question related to react hooks. The question illustrated below: Design a custom hook to encapsulation useEffect which can perform side-effects operations(such as AJAX request), the designed custom hook can skip the first operation and begin to perform side-effects operations from the second time.
Here I designed one hook, however it is not correct. I do know the key for the hook is how to judge whether this is the first time that the custom hook is called. Someone told me that I should use useRef
to count the counter
, but I'm not quiet sure how to design it with
useRef
. Could you guys help me with this question?
function useMyCoustomHook(func, dependencies){
let counter = 1;
useEffect(()=>{
if(counter === 1){
counter--;
}else{
func();
}
}, [dependencies])
}