2

I want to get the line-height of the element in Rect after render and for this I'm using useRef

const titleLineHeight = titletext.current?.style.lineHeight;

and I didn't get any value in CSSStyleDeclaration

enter image description here

Im not sure how to get the values. Please help me

Thanks in advance :)

Kamal
  • 2,140
  • 8
  • 33
  • 61
  • Does this answer your question? [How to Access styles from React?](https://stackoverflow.com/questions/35170581/how-to-access-styles-from-react) – Eldar B. Nov 02 '21 at 07:41

1 Answers1

1

use getComputedStyle()

https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle


const paraRef = useRef(null);

  useEffect(() => {
    const paraStyle = paraRef.current && getComputedStyle(paraRef.current);
    console.log(paraStyle.lineHeight);
  }, []);

  return (
    <div className="App">
      <p ref={paraRef}>New text</p>
    </div>
  );

Madhuri
  • 1,060
  • 5
  • 17