How to get parent width/height in React using Hooks?

I'm creating a component and I need to get it's parent <div> width and height. I'm using Hooks, so all my components are functions. I've read some examples using classes, but this won't apply to my component.

So I have this component:

export default function PlantationMap(props) {
    <div className="stage-canvas">
          width={window.innerWidth * 0.5}
          height={window.innerHeight * 0.5}
        / >

How could I get the <div> height and width to use in <Stage width={} height={} />?

Thank you very much in advance

Edit: I tried using the useRef() hook, like this:

const div = useRef();

return (
  <div ref={div}>

But I can't access the div.current objectShareImprove this questionFollowedited Oct 3, 2019 at 15:16asked Oct 3, 2019 at 15:08

user avatar

Otavio Bonder1,41922 gold badges1212 silver badges2727 bronze badges

5 AnswersSorted by:                                              Highest score (default)                                                                   Date modified (newest first)                                                                   Date created (oldest first)                              20

I think useCallback is what you want to use so you can get the width and height when it changes.

  const [height, setHeight] = useState(null);
  const [width, setWidth] = useState(null);
  const div = useCallback(node => {
    if (node !== null) {
  }, []);

  return (
    <div ref={div}>