Example:
<Suspense fallback={<ClearLoading />}>
// stuff
</Suspense>
To track if the whole suspense node finishes loading, I put an effect cleanup in <ClearLoading>
like this:
const ClearLoading = () => {
useEffect(() => {
return () => {
// Do sth when suspense finish
}
}, []);
return null;
};
This seems stupid. What is the generally accepted way of doing this?
2
Answers
Using the
useEffect
cleanup function in yourClearLoading
component is indeed a bit unconventional for tracking when a Suspense boundary finishes loading.While it’s a valid approach, it can feel a bit clunky and messy. We can handle this scenario in React by following this approaches:
Using
useEffect
is the idiomatic approach, as mentioned in React 18 upgrade guide.However, by having it be an empty child component of Suspense, you wont need a wrapper for every Loading component.
The tracking-component doesnt need a return value.