React Strict Mode

React StrictMode

触发时机: 开发模式, 并且调用StrictMode

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  //<App />,
  document.getElementById('root')
);

作用

  • 识别不安全的生命周期
  • 关于使用过时字符串 ref API 的警告
  • 关于使用废弃的 findDOMNode 方法的警告
  • 检测意外的副作用
  • 检测过时的 context API

检测意外的副作用

这些方法会被调用两次

  • constructor
  • componentWillReceiveProps
  • componentWillUpdate
  • getDerivedStateFromProps
  • shouldComponentUpdate
  • render
  • setState 更新函数(第一个参数)
  • FunctionalComponent 会被执行两遍

rc-trigger 里面就有个bug
https://github.com/react-component/trigger/issues/264

原文地址:https://www.cnblogs.com/kongshu-612/p/14802502.html