![](/img/trans.png)
[英]In functional component useContext works in console.log but returns error on render
[英]In a functional component, where would I place a console.log to test if the component rerenders?
如何檢查功能組件何時更新和/或重新呈現?
在類組件中,我可以使用componentWillUpdate
來console.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”。
請注意以下生命周期方法:
不再鼓勵。 閱讀更多
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.