In reacting whatever we write that looks like HTML is not pure HTML actually. All the HTML looking stuff are JSX, Behind the scene, they are converted to vanilla JavaScript using babel. These all work in this way to make the developers’ life easier. Since JSX are not HTML that’s why we do have any direct reference to the HTML elements and that’s why we can’t direct fetch properties of any HTML element. To fetch the elements’ property, React gives something called as ‘ref’. Using ref we can create a direct reference to any HTML elements and get controlled over HTML elements properties. Here we use the ‘ref’ system to fetch image height and width.
Example 1: This example illustrates how to fetch the current height and width of the image.
-
index.js:
Javascript
import React from'react'import ReactDOM from'react-dom'import App from'./App'ReactDOM.render(<App />, document.querySelector('#root'))chevron_rightfilter_none -
App.js:
Javascript
import React, { Component } from'react'class App extends Component{constructor(props){super(props)// Initialize count statethis.state = {show :false}// Bind context of 'this'this.handleClick =this.handleClick.bind(this)// Create reference of DOM objectthis.imgRef = React.createRef()}renderDetails() {returnthis.state.show ?// Accessing image details using imgRef<div><p><strong>Height : </strong>{this.imgRef.current.clientHeight}px</p><p><strong>Width : </strong>{this.imgRef.current.clientWidth}px</p></div> :null}handleClick(){// Update state valuethis.setState({show :true})}render(){return(<div><h3>GeeksforGeeks</h3>{/* Assign reference to DOM element */}<img ref={this.imgRef} src='https://media.geeksforgeeks.org/wp-content/uploads/20200617121258/gfg-image2-300x177.png'alt='gfg'/><div><button onClick={this.handleClick}>Get image details</button></div>{this.renderDetails()}</div>)}}exportdefaultAppchevron_rightfilter_none
Output :

Example 2:
-
index.js:
Javascript
import React from'react'import ReactDOM from'react-dom'import App from'./App'ReactDOM.render(<App />, document.querySelector('#root'))chevron_rightfilter_none -
App.js:
Javascript
import React, { Component } from'react'class App extends Component{constructor(props){super(props)// Initialize count statethis.state = {height:null, width:null, isIncrease:false}// Bind context of 'this'this.handleClick =this.handleClick.bind(this)// Create reference of DOM objectthis.imgRef = React.createRef()}handleClick(){// Fetching current height and widthconst height =this.imgRef.current.clientHeightconst width =this.imgRef.current.clientWidthalert(`Height : ${height}Width : ${width}`)}render(){return(<div><h3>GeeksforGeeks</h3>{/* Assign reference to DOM element */}<img ref={this.imgRef} src='https://media.geeksforgeeks.org/wp-content/uploads/20200819170816/colorfulbulmaheroelements-300x154.png'alt='gfg'/><div><button onClick={this.handleClick}>Fetch dimension</button></div></div>)}}exportdefaultAppchevron_rightfilter_none
Output :

Recommended Posts:
- HTML width/height Attribute vs CSS width/height Property
- How to get the image size (height & width) using JavaScript ?
- How to get the width and height of an image ?
- How to set the width and height of an image using HTML ?
- How to set the height and width of background image inline style in react?
- How to animate div width and height on mouse hover using jQuery ?
- How to calculate Width and Height of the Window using JavaScript?
- How to specify the width and height of the canvas using HTML ?
- How to dynamically set the height and width of a div element using jQuery ?
- How to zoom-in and zoom-out image using ReactJS?
- CSS | Height and Width
- How to change the width and height of Twitter Bootstrap's tooltips?
- How to get the new image URL after refreshing the image using JavaScript ?
- How to get the width of scroll bar using JavaScript ?
- How to dynamically get the content width of a div using AngularJS ?
- How to get the <div> height using JavaScript ?
- How to dynamically get the content height of a div using AngularJS ?
- How to get the height of scroll bar using JavaScript ?
- CSS | border-image-width Property
- HTML | DOM Input Image width Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


