0

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

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

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

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

4

2 回答 2

1

使用 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`;
  });
于 2019-11-30T19:10:51.307 回答
0

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

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

  • 组件WillMount
  • 组件WillReceiveProps
  • 组件WillUpdate

不再鼓励。 阅读更多

于 2019-11-30T21:16:54.127 回答