How To Get The Height And Width Of An Image Using ReactJS

We’ll learn how to use ReactJS to obtain the height and width of an image in this tutorial.

In responding, whatever we write that appears to be HTML isn’t truly HTML.

All of the HTML-like elements are JSX, which is translated to vanilla JavaScript behind the scenes using babel.

All of these things work together to make the developers’ lives easier. We don’t have any direct references to HTML elements because JSX isn’t HTML, and we can’t directly fetch properties of any HTML element. React provides a function called ‘ref’ to get the properties of the elements.

You can see the example below.

import React, { Component } from 'react'

class Index extends Component {
    constructor(props) {
        this.state = { show: false }
        this.handleClick = this.handleClick.bind(this)
        this.imgRef = React.createRef()

    renderDetails() {
        return ?
                <p><strong>Height : </strong>
                <p><strong>Width : </strong>
            </div> : null

    handleClick() {
            show: true

    render() {
        return (
                <h3>How to get image width and height in ReactJS</h3>
                <img ref={this.imgRef} src=
                    '' alt='gfg' />
                    <button onClick={this.handleClick}>Get image details</button>
export default Index

You can see the output below.

Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories