I'm look at implementing a loading screen that checks if all components are loaded, but came across this weird pattern. With the code below, I get App false
in console to start, after 2000ms when the delayHTML function returns back data, I get LoadComponent mount event
and App true
.
But then it goes back into the suspense fallback, and I get a further 2000ms where the LoadComponent mount event
and App true
messages appear again, in what looks like a 2nd render. How can I avoid that?
function App() {
const [fullLoadState, setfullLoadState] = useState(false);
console.log("App " + fullLoadState)
eventBus.on("FullyLoaded", (data) => setfullLoadState(true));
return (
<div>
<Suspense fallback='Fallback'>
<LoadComponent resource={delayHTML(2000)}/>
</Suspense>
<p>{fullLoadState ? null : "Site not fully loaded" }</p>
</div>
);
}
function LoadComponent({resource}){
useEffect(()=> {
console.log ('LoadComponent mount event')
eventBus.dispatch("FullyLoaded", {messsage: 'some message'});
})
const html = resource.read();
return html;
}
export default App;
EventBus is just EventListener. delayHTML suspends until it reaches 2000ms to return a text value.