In an isomorphic page rendering, the image can be downloaded before the main script.js file. Thus, the image may already be loaded before the react register of the react event - do not trigger this event.
script.js
constructor(props) { super(props); this.handleImageLoaded = this.handleImageLoaded.bind(this); } handleImageLoaded() { console.log('image loaded'); } render() { return ( <img src='image.jpg' onLoad={this.handleImageLoaded} /> ); }
Scenario 1 - image.jpg bigger than script.js

In this scenario, everything is working fine. The event is logged before the image is finally loaded, so the image loaded message is in the image loaded console.
Scenario 2 - image.jpg smaller than script.js

In this scenario, you can see the problem described at the beginning of the post. The onLoad event is not raised.
Question
What can I do to trigger the onLoad event in script 2 ?
EDIT: Pull implementation response
To determine if an image is ready when rendering, you should check the complete property on a clean img javascript object:
constructor(props) { super(props); this.state = { loaded: false }; this.handleImageLoaded = this.handleImageLoaded.bind(this); this.image = React.createRef(); } componentDidMount() { const img = this.image.current; if (img && img.complete) { this.handleImageLoaded(); } } handleImageLoaded() { if (!this.state.loaded) { console.log('image loaded'); this.setState({ loaded: true }); } } render() { return ( <img src='image.jpg' ref={this.image} onLoad={this.handleImageLoaded} /> ); }
javascript reactjs isomorphic-javascript
Everettss
source share