From 55f3df293ae1f588321b641e8c5eb75237e0574e Mon Sep 17 00:00:00 2001 From: max-voronov <70445727+max-voronov@users.noreply.github.com> Date: Wed, 30 Sep 2020 12:22:20 +0300 Subject: [PATCH] chore(client): refactor ImageLoader (#39691) --- client/src/components/helpers/ImageLoader.js | 79 ++++++++------------ 1 file changed, 31 insertions(+), 48 deletions(-) diff --git a/client/src/components/helpers/ImageLoader.js b/client/src/components/helpers/ImageLoader.js index 7a4342ef9d..78e188ea06 100644 --- a/client/src/components/helpers/ImageLoader.js +++ b/client/src/components/helpers/ImageLoader.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState } from 'react'; import './image-loader.css'; import LazyLoad from 'react-lazy-load'; @@ -14,52 +14,35 @@ const propTypes = { width: PropTypes.number }; -class ImageLoader extends React.Component { - // initial state: image loaded stage - state = { - loaded: false - }; - - // define our loading and loaded image classes - static defaultProps = { - className: '', - loadingClassName: 'img-loading', - loadedClassName: 'img-loaded' - }; - - // image onLoad handler to update state to loaded - onLoad = () => { - this.setState(() => ({ loaded: true })); - }; - - render() { - let { - className, - loadedClassName, - loadingClassName, - alt, - src, - width, - height - } = this.props; - - className = `${className} ${ - this.state.loaded ? loadedClassName : loadingClassName - }`; - - return ( - - {/* eslint-disable jsx-a11y/no-noninteractive-element-interactions */} - {alt} - {/* eslint-enable jsx-a11y/no-noninteractive-element-interactions */} - - ); - } -} +const ImageLoader = ({ + className = '', + loadedClassName = 'img-loaded', + loadingClassName = 'img-loading', + alt, + src, + width, + height +}) => { + const [loaded, setLoaded] = useState(false); + const fullClassName = `${className} ${ + loaded ? loadedClassName : loadingClassName + }`; + return ( + + {/* eslint-disable jsx-a11y/no-noninteractive-element-interactions */} + {alt} setLoaded(true)} + src={src} + /> + + ); +}; ImageLoader.propTypes = propTypes; export default ImageLoader;