简体   繁体   English

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

[英]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.在类组件中,我可以使用componentWillUpdateconsole.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:请注意以下生命周期方法:

  • componentWillMount componentWillMount
  • componentWillReceiveProps componentWillReceiveProps
  • componentWillUpdate componentWillUpdate

are no longer encouraged.不再鼓励。 Read more 阅读更多

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

相关问题 在功能组件中,useContext 在 console.log 中工作,但在渲染时返回错误 - In functional component useContext works in console.log but returns error on render 在哪里写console.log里面的组件? 反应 - Where to write console.log inside component ? react 登录组件在console.log上提供未定义 - Sign In Component gives undefined on console.log 里面的console.log<errormessage> 零件</errormessage> - console.log inside <ErrorMessage> component 带有 React.memo() 的功能组件仍然会重新渲染 - Functional component with React.memo() still rerenders 为什么我的 React 组件不会在 foreach 中呈现我想要的内容,但会使用 console.log 记录数组? - Why doesn't my React component renders what I want in a foreach, but will console.log the array? this.props在子组件的console.log中工作,但不渲染 - this.props working in console.log of child component, but not rendering 为什么子组件不显示console.log输出? - why child component not showing console.log Outputs? 为什么 `Promise.then` 在 React 组件中被调用两次,而不是在 console.log 中? - Why is `Promise.then` called twice in a React component but not the console.log? 为什么在React Component构造函数中的console.log(super)会抛出错误? - Why console.log(super) in React Component constructor throw an Error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM