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:
preloaderimage
loads- when loaded
imageloadhandler
triggers ,src
attribute changed mainimage
src (sopreloaderimage
isn't rendered loaded) - now primary image loads
- when loaded rendered
how should properly:
if write react app, should react way:
set images component
state
inconstructor
method. need because gonna change way images rendered (in react achieve render changes via changing either component'sprops
orstate
, in case shouldstate
). notice should setsrc
property each image in listpreloaderimage
(in example below usingobject destructuring
syntax).constructor() { super(); this.state = { images: this.props.images.map(image => ({ ...image, src: preloaderimage })) } }
now in
render
method create image list (note, don't needonload
handler here):const listwork = this.state.images.map(image => ( <li key={image.id}> <div> <img src={image.src} /> </div> </li> ));
in
componentwillmount
method start load primary imagessrc
dynamically using vanilla js (you can read articlehere
). loaded callsetstate
method update imagessrc
property: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
setstate
called changedimages
array react automatically rerender images correctsrc
path.
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