簡體   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