reactjs - How am I suppose to utilise the onLoad method to load images in React? -
i hope can help. i'll try , keep concise can.
my goal have gallery images render in load.
looking online, have learned onload method.
have tried utilise, idea being preloader.gif render until image loaded , replace image src loaded image.
as now(below), images seem load in same way if didn't use onload method there moment no image being rendered @ before images load in.
what doing wrong exactly?
i've stripped down code make question easier read.
import react 'react'; import reactdom 'react-dom'; import preloaderimage '../../../img/site/preloader.gif'; class workspage extends react.component { constructor(){ super(); } imageloadhandler(image,e) { e.target.src = image; } render() { const listwork = this.props.array.map((item,index)=> { return( <li key={item.id}> <div> <img src={preloaderimage} onload={this.imageloadhandler.bind(this,item.image)}/> </div> </li> ) }); return( <li> <div> <ul> {listwork} </ul> </div> </li> ) } } module.exports = workspage; your appreciated.
thank you
moe
here explanation why preloader image isn't rendered:
preloaderimageloads- when loaded
imageloadhandlertriggers ,srcattribute changed mainimagesrc (sopreloaderimageisn't rendered loaded) - now primary image loads
- when loaded rendered
how should properly:
if write react app, should react way:
set images component
stateinconstructormethod. need because gonna change way images rendered (in react achieve render changes via changing either component'spropsorstate, in case shouldstate). notice should setsrcproperty each image in listpreloaderimage(in example below usingobject destructuringsyntax).constructor() { super(); this.state = { images: this.props.images.map(image => ({ ...image, src: preloaderimage })) } }now in
rendermethod create image list (note, don't needonloadhandler here):const listwork = this.state.images.map(image => ( <li key={image.id}> <div> <img src={image.src} /> </div> </li> ));
in
componentwillmountmethod start load primary imagessrcdynamically using vanilla js (you can read articlehere). loaded callsetstatemethod update imagessrcproperty:componentwillmount() { this.state.images.foreach((image, index) => { const {src} = this.props.images[index] // image primary src const primaryimage = new image() // create image object programmatically primaryimage.onload = () => { // use arrow function here console.log(`image #${index + 1} loaded!`) const images = [...this.state.images] // copy images array state images[index].src = src // adjust loaded image src this.setstate({ images }) } primaryimage.src = src // after set onload handler }) }after
setstatecalled changedimagesarray react automatically rerender images correctsrcpath.
more elegant way use of css:
instead of <img /> tag can use <div /> tag background-image css property. there can set fallback (preload) image:
background-image: url('path/to/primary-image'), url('path/to/preload-image'); you can set paths dynamically style property (in render method):
const listwork = this.props.images.map(image => { const imagestyle = { backgroundimage: `url(${image.src}), url(${preloaderimage});` } return ( <li key={image.id}> <div> <div style={imagestyle} /> </div> </li> ) }) just don't forget set height , width <div />.
Comments
Post a Comment