I'm trying to preload an image in useEffect
, but it doesn't seem to be downloading the image when app component mounts.
I'm doing something like the following:
export default function App() {
const [show, setShow] = React.useState(false)
React.useEffect(() => {
const image = new Image();
image.src =
"https://images.unsplash.com/photo-1601758065893-25c11bfa69b5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1279&q=80";
setTimeout(() => setShow(true), 10000)
}, [])
return (
<>
{show && (<img src="https://images.unsplash.com/photo-1601758065893-25c11bfa69b5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1279&q=80 />)}
</>
)
The idea here is to download the image ahead of time, so that way when the img
is to be displayed, it wouldn't have to download the media.
Unfortunately, useEffect
doesn't seem to be triggering the image download. Why doesn't it fetch the image when the component mounts?