首页 > 解决方案 > 在功能组件中,我应该在哪里放置 console.log 来测试组件是否重新呈现?

问题描述

如何检查功能组件何时更新和/或重新呈现?

在一个类组件中,我可以使用componentWillUpdate一个console.log消息。我将在哪里放置console.log功能组件以获得相同的控制台输出?

我看到两个地方,组件的函数体,我会把它放在开头。
在 jsx 内部。那些会给我同样的结果吗?

我在问,因为我无法React.memo上班,我想知道我是否测试了错误的东西。

标签: javascriptreactjsreact-functional-component

解决方案


使用 React 16.8 官方文档中的效果挂钩

function Example() {
  const [count, setCount] = useState(0);

  // Similar to componentDidMount and componentDidUpdate:
  useEffect(() => {
    // Update the document title using the browser API
    document.title = `You clicked ${count} times`;
  });

推荐阅读