Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactjs - Get Height of a div/image using React Hooks

I want to get Height of an image in a react functional component using react hooks.

I have used below code:

import React, { useRef, useEffect } from 'react'

const DepthChartContent = props => {
  const ref = useRef()

  useEffect(() => {
    if (ref && ref.current && ref.current.clientHeight) {
      console.log('called')
    }
    console.log('useEffect', {
      ref,
      current: ref.current,
      clientHeight: ref.current.clientHeight,
    })
  }, [])

  return (
    <img
      ref={ref}
      className="depth-reference-bar"
      src={DepthRuler}
      alt="no ruler found"
    />
  )
}

The problem with this is that it returns the clientHeight as 0 but console.log in useEffect has the correct clientHeight as shown in below pic.

ref-console-image

This means that ref && ref.current && ref.current.clientHeight is not defined when called but consoling in same useEffect is showing correct value for ref, current: ref.current but clientHeight: ref.current.clientHeight is ZERO.

Similarly, I can't use ....}, [ref && ref.current && ref.current.clientHeight] in useEffect because useEffect do not accept complex expression. If I defined a variable outside or inside useEffect as const clientHeight = (ref && ref.current && ref.current.clientHeight) || 0, no luck!

Can anyone help in this regards. Thanks!

like image 697
Owais Avatar asked May 18 '20 07:05

Owais


People also ask

How do you find the height of a div in React Hooks?

To get the parent height and width in React: Set the ref prop on the element. In the useEffect hook, update the state variables for the height and width. Use the offsetHeight and offsetWidth properties to get the height and width of the element.

How do you find the height and width of an image in React?

To get the dimensions of image with React, we can get it from the load event handler of the image. We define the onImgLoad function that gets the image from the target property. Then we destructure the offsetHeight and offsetWidth properties from the image. Next, we log the height and width with console.

How do you find the height of a React?

To get the height of an element with React, we can assign a ref to the element we want to get the height for. Then we can use the clientHeight property to get the height. We call the useRef hook and assign the returned ref to elementRef . Then we set elementRef as the value of the ref prop of the div.


1 Answers

As others mentioned here, your logic happens before image is loaded in most cases. So you have to get the image dimensions after the image has loaded because the browser doesn't know the image dimensions before that.

I know you mentioned you want to do it with hooks, but unless you are doing something reusable, there's probably no need for hooks here. Or unless you are trying to understand them better, then it's fine.

One option would be to just use the onLoad event on the image element. This way you don't have to do all these checks if you have something in ref.current or is img.complete === true.

import React from 'react'

const DepthChartContent = props => {
  const handleImageLoad = (event) => {
    // Do whatever you want here
    const imageHeight = event.target.clientHeight;
  }

  return (
    <img
      ref={ref}
      className="depth-reference-bar"
      src={DepthRuler}
      alt="no ruler found"
      onLoad={handleImageLoad}
    />
  )
}

Don't get me wrong, I absolutely love hooks, but they aren't always the solution.

like image 199
Marko Hologram Avatar answered Nov 06 '22 09:11

Marko Hologram