In isomorphic rendered page image can be downloaded before main script.js
file. So image can be already loaded before react
register onLoad
This is all a bit tidier with Hooks:
const useImageLoaded = () => {
const [loaded, setLoaded] = useState(false)
const ref = useRef()
const onLoad = () => {
setLoaded(true)
}
useEffect(() => {
if (ref.current && ref.current.complete) {
onLoad()
}
})
return [ref, loaded, onLoad]
}
const SomeComponent = ({ src }) => {
const [ref, loaded, onLoad] = useImageLoaded()
return (
<div>
<img ref={ref} onLoad={onLoad} src={src} alt="" />
{loaded && <h1>Loaded!</h1>}
</div>
)
}
Another way is to use ref and cover those both scenarios:
<img
ref={(input) => {
// onLoad replacement for SSR
if (!input) { return; }
const img = input;
const updateFunc = () => {
this.setState({ loaded: true });
};
img.onload = updateFunc;
if (img.complete) {
updateFunc();
}
}}
src={imgSrc}
alt={imgAlt}
/>
You could check the complete
property on the image before applying the onload
event.
if (!img.complete) {
// add onload listener here
}
<img
src={this.props.imageUrl}
onLoad={this.handleImageLoaded.bind(this)}
onError={this.handleImageErrored.bind(this)}
/>
img.complete is true even when the src load fails.
complete - Returns a Boolean that is true if the browser has finished fetching the image, whether successful or not. It also shows true, if the image has no src value.
state = {
isLoading: true,
hasError: false,
}
myRef = React.createRef();
componentDidMount() {
const img = this.myRef.current;
if (img && img.complete) {
if (img.naturalWidth === 0) {
this.handleOnError();
} else {
this.handleImageLoaded();
}
}
}
handleImageLoaded = () => {
if (this.state.isLoading) {
this.setState({ isLoading: false });
}
}
handleOnError = () => {
this.setState({ hasError: true });
}
render() {
return (
<img
src={src}
alt={alt}
ref={this.myRef}
onError={this.handleOnError}
onLoad={this.handleOnLoad}
/>
);
}
componentDidMount() {
const testImg = new Image();
testImg.onerror = this.handleOnError;
testImg.onload = this.handleImageLoaded;
testImg.src = this.props.src; // important to set eventlisteners before src
}