So, I built a custom hook to fetch data from an api. Here is the code:
export const useLambdaApi = () => {
const [data, setData] = useState()
const [isLoading, setIsLoading] = useState(false)
useEffect(() => {
const fetchData = async () => { ... }
fetchData();
},[isLoading]);
return [data, setIsLoading];
}
And in the component I need the data I do:
export default function Comp (props) {
const [data, setIsLoading] = useLambdaApi()
useEffect(() => {
const interval = setInterval(() => {
setIsLoading(true)
console.log(Date())
}, 10000);
return () => {
window.clearInterval(interval); // clear the interval in the cleanup function
};
},[data]);
return( ... )
}
But I get a TypeError: TypeError: setIsLoading is not a function
I know this must be something silly, but I am relatively new to React, so any feedback would be of much help.
Thanks.
EDIT:
To provide more context I added more code to my snipped of the component. I try to update the isLoading
state from a setInterval
. But I also did try from useEffect
without the interval, and outside of useEffect...
This is the Stack trace:
PatientBoard.js:26 Uncaught TypeError: setIsLoading is not a function
at PatientBoard.js:26
(anonymous) @ PatientBoard.js:26
setInterval (async)
(anonymous) @ PatientBoard.js:25
commitHookEffectList @ react-dom.development.js:21100
commitPassiveHookEffects @ react-dom.development.js:21133
callCallback @ react-dom.development.js:363
invokeGuardedCallbackDev @ react-dom.development.js:412
invokeGuardedCallback @ react-dom.development.js:466
flushPassiveEffectsImpl @ react-dom.development.js:24223
unstable_runWithPriority @ scheduler.development.js:676
runWithPriority$2 @ react-dom.development.js:11855
flushPassiveEffects @ react-dom.development.js:24194
(anonymous) @ react-dom.development.js:23755
scheduler_flushTaskAtPriority_Normal @ scheduler.development.js:451
flushTask @ scheduler.development.js:504
flushWork @ scheduler.development.js:637
performWorkUntilDeadline @ scheduler.development.js:238