Search code examples
reactjsreact-component

react-sizeme doesn't measure height of component


I am using react-sizeme in order to measure the height/width of a Content component from ant design. Here is my code:

 <SizeMe render={({ size }) => {
            if (size.width == null && size.height == null) {
              return <Content></Content>
            } else {
              //Content contains a lot of HTML, so I only want to fully render it after the inital size has been determined
              return <Content>Width: {size.width} Height: {size.height}</Content>
            }
          }} />

However, size.height is undefined/null. Why is the height not being measured, and how can I fix this problem?


Solution

  • By default, SizeMe won't track the height. You can fix this by adding monitorHeight:

    import React from "react";
    import ReactDOM from "react-dom";
    import { SizeMe } from "react-sizeme";
    
    function App() {
      return (
        <SizeMe
          monitorHeight
          render={({ size }) => {
            return <h1>I am {size.height}px tall!</h1>;
          }}
        />
      );
    }
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<App />, rootElement);
    

    You can see the available options on the Github repo here.