react-sizeme 不测量组件的高度

Roy*_*son 2 reactjs react-component

我正在使用 react-sizeme 来测量Content来自 ant design的组件的高度/宽度。这是我的代码:

 <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>
            }
          }} />
Run Code Online (Sandbox Code Playgroud)

但是,size.height未定义/空。为什么没有测量高度,我该如何解决这个问题?

Col*_*rdo 5

默认情况下,SizeMe不会跟踪高度。您可以通过添加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);
Run Code Online (Sandbox Code Playgroud)

您可以在此处查看Github 存储库中的可用选项。