为什么我的create-react-app console.log两次?

为什么我的create-react-app console.log两次?,第1张

为什么我的create-react-app console.log两次?

这是有目的的,它是

React.StrictMode
(专门用于检测意外副作用)的一部分:

严格模式无法自动为您检测副作用,但可以通过使其更具确定性来帮助您发现它们。这是通过有意地双重调用以下功能来完成的:

  • 类成分
    constructor
    render
    shouldComponentUpdate
    方法
  • 组件静态
    getDerivedStateFromProps
    方法
  • 功能组件主体
  • 状态更新器功能(的第一个参数
    setState
  • 函数传递给
    useState
    useMemo
    useReducer


如果

StrictMode
从中删除元素
index.js
,您将看到输出仅记录一次:

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

请注意,在严格模式下,这仅在 开发中 发生,而不在生产中发生。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5652008.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存