使 React 使用效果钩子不在初始渲染时运行
2022-08-30 00:31:56
						根据文档:
componentDidUpdate()在更新发生后立即调用。初始呈现不调用此方法。
我们可以使用新的钩子来模拟 ,但它似乎在每次渲染后运行,即使是第一次渲染。如何让它不在初始渲染上运行?useEffect()componentDidUpdate()useEffect()
如下面的示例所示,在初始渲染期间打印,但在初始渲染期间未打印。componentDidUpdateFunctioncomponentDidUpdateClass
function ComponentDidUpdateFunction() {
  const [count, setCount] = React.useState(0);
  React.useEffect(() => {
    console.log("componentDidUpdateFunction");
  });
  return (
    <div>
      <p>componentDidUpdateFunction: {count} times</p>
      <button
        onClick={() => {
          setCount(count + 1);
        }}
      >
        Click Me
      </button>
    </div>
  );
}
class ComponentDidUpdateClass extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0,
    };
  }
  componentDidUpdate() {
    console.log("componentDidUpdateClass");
  }
  render() {
    return (
      <div>
        <p>componentDidUpdateClass: {this.state.count} times</p>
        <button
          onClick={() => {
            this.setState({ count: this.state.count + 1 });
          }}
        >
          Click Me
        </button>
      </div>
    );
  }
}
ReactDOM.render(
  <div>
    <ComponentDidUpdateFunction />
    <ComponentDidUpdateClass />
  </div>,
  document.querySelector("#app")
);
<script src="https://unpkg.com/react@16.7.0-alpha.0/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16.7.0-alpha.0/umd/react-dom.development.js"></script>
<div id="app"></div>