繁体   English   中英

在功能组件中,我应该在哪里放置 console.log 来测试组件是否重新呈现?

[英]In a functional component, where would I place a console.log to test if the component rerenders?

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

在类组件中,我可以使用componentWillUpdateconsole.log消息。 我应该将console.log放在功能组件中的什么位置以获得相同的控制台输出?

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

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

使用 React 16.8官方文档中的 effect hooks

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`;
  });

所以你说你可以使用“ componentWillUpdate to console.log a message”。

请注意以下生命周期方法:

  • componentWillMount
  • componentWillReceiveProps
  • componentWillUpdate

不再鼓励。 阅读更多

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM