I would like to trigger a dynamic import of a react component only after an image is loaded to prioritise first paint. The problem is, it's a context provider component and having some trouble as a result.
So I have a dynamic import here in the file auth.js:
const AuthContextPreloader = hasWindow
? lazy(() => import("./AuthContextPreloader"))
: null;
And I have an image here in a separate component:
<img
ref={() => hasWindow && imageLoaded()}
className={styles.optionsImageImg}
alt={"nav"}
src={didLoad && thumb.jpg}
type="image/jpeg"
/>
And once loaded I a send state up the component tree to hand down to AuthContextPreloader
const imageLoaded = () => {
setheroLoaded(true);
};
Some pseudo-code to try and achieve this in auth.js:
useEffect(() => {
props.heroLoaded && **trigger the lazyload**;
}, [props.heroLoaded]);
But totally unsure how to implement that. Thank you.