Get image sizes using React

I need to get image sizes using React. I found a library called reactive measure that calculates the dimensions of React components. It works, but I can not start it when the image has loaded. I need to make it light up when the image loads, so I get the exact dimensions, not 0 x 157 or something like that.

I tried using onLoadimages onLoadto detect when the image was loading, but I did not get satisfactory results. Essentially, what I did when the image handleImageLoaded()was loaded ( was called), change the hasLoadedstate property to true. I know it hasLoadedhas been changed to truebecause he says so: Image Loaded: true.

I noticed that I can only calculate sizes for images that have been cached ...

Here is a demo video : cl.ly/250M2g3X1k21

Is there a better, more concise way to get measurements with React?

Here is the code:

import React, {Component} from 'react';
import Measure from '../src/react-measure';
class AtomicImage extends Component {

    constructor() {
        super();
        this.state = {
            hasLoaded: false,
            dimensions: {}
        };
        this.onMeasure = this.onMeasure.bind(this);
        this.handleImageLoaded = this.handleImageLoaded.bind(this);
    }

    onMeasure(dimensions) {
        this.setState({dimensions});
    }

    handleImageLoaded() {
        this.setState({hasLoaded: true});
    }

    render() {

        const {src} = this.props;
        const {hasLoaded, dimensions} = this.state;
        const {width, height} = dimensions;

        return(
            <div>
                <p>Dimensions: {width} x {height}</p>
                <p>Image Loaded: {hasLoaded ? 'true' : 'false'}</p>
                <Measure onMeasure={this.onMeasure} shouldMeasure={hasLoaded === true}>
                    <div style={{display: 'inline-block'}}>
                        <img src={src} onLoad={this.handleImageLoaded}/>
                    </div>
                </Measure>
            </div>
        );
    }
}

export default AtomicImage;

Here is the parent code. This is not very important - it simply passes to the srcelement AtomicImage:

import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import AtomicImage from './AtomicImage';

class App extends Component {

  constructor() {
    super();
    this.state = {src: ''}
    this.handleOnChange = this.handleOnChange.bind(this);
  }

  handleOnChange(e) {
    this.setState({src: e.target.value});
  }

  render() {

    const {src} = this.state;
    return (
      <div>
        <div>
          <input onChange={this.handleOnChange} type="text"/>
        </div>
        <AtomicImage src={src} />
      </div>
    )
  }

}

ReactDOM.render(<App />, document.getElementById('app'));
+8
source share
1

js: offsetHeight, offsetWidth. img, img . img.

: https://jsbin.com/vufegicota/edit?js,output

class AtomicImage  extends Component {
   constructor(props) {
        super(props);
        this.state = {dimensions: {}};
        this.onImgLoad = this.onImgLoad.bind(this);
    }
    onImgLoad({target:img}) {
        this.setState({dimensions:{height:img.offsetHeight,
                                   width:img.offsetWidth}});
    }
    render(){
        const {src} = this.props;
        const {width, height} = this.state.dimensions;

        return (<div>
                dimensions width{width}, height{height}
                <img onLoad={this.onImgLoad} src={src}/>
                </div>
               );
    }
}
+14

Source: https://habr.com/ru/post/1652134/


All Articles