[英]In a functional component, where would I place a console.log to test if the component rerenders?
How I can check when a functional component updates and/or rerenders?如何检查功能组件何时更新和/或重新呈现?
In a class component, I can use componentWillUpdate
to console.log
a message.在类组件中,我可以使用
componentWillUpdate
来console.log
消息。 Where would I place the console.log
in a functional component to get the same console output?我应该将
console.log
放在功能组件中的什么位置以获得相同的控制台输出?
I see two places, the function body of the component, I would put it right at the start.我看到两个地方,组件的函数体,我会把它放在开头。
And inside the jsx.在 jsx 里面。 Would those give me the same result?
那些会给我同样的结果吗?
I am asking since I can't get React.memo
to work, and I am wondering if I test the wrong thing.我问是因为我无法让
React.memo
工作,我想知道我是否测试了错误的东西。
Use the effect hooks in React 16.8 offical document使用 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`;
});
So you stated you could use " componentWillUpdate to console.log a message."所以你说你可以使用“ componentWillUpdate to console.log a message”。
Please note that the following lifecycle methods:请注意以下生命周期方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.