简体   繁体   English

React 严格模式和双重渲染

[英]React strict mode and double rendering

Having such a simple CRA React app:拥有这样一个简单的 CRA React应用程序:

./index.tsx: ./index.tsx:

import React from 'react';
import ReactDOM from 'react-dom';
import Home from './components/Home';

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

./components/Home.tsx: ./components/Home.tsx:

function Home() {
    let myPromise = new Promise(function(myResolve, myReject) {
        
        let x = 0;
        console.log("--> inside a Promise obj <--");
        
        if (x === 0) {
            myResolve("OK");
        } else {
            myReject("Error");
        }
    }); 

    myPromise.then(
        function(value) {console.log("--> Promise fulfilled <--")},
    );

    return (
        <div>
            <b>Home Page</b>
        </div>
    )
}

export default Home

I'm getting:我越来越:

--> inside a Promise obj <--
--> Promise fulfilled <--
--> Promise fulfilled <--

on the console.在控制台上。

Why does the Promise fulfilled code is triggered 2 times?为什么Promise 已完成代码被触发 2 次? I know, that for React Strict mode the component is rendered twice but the example show some inconsistency here - the --> inside a Promise obj <-- message is called ONE time while the --> Promise fulfilled <-- TWICE?我知道,对于React Strict模式,组件被渲染了两次,但示例在这里显示了一些不一致 - --> inside a Promise obj <--消息被调用一次,而--> Promise fulfilled <-- although both messages is being printed in the same function!?尽管两条消息都在同一个函数中打印!?

According to the docs :-根据文档:-

Starting with React 17, React automatically modifies the console methods like console.log() to silence the logs in the second call to lifecycle functions.从 React 17 开始,React 会自动修改 console.log() 等控制台方法,以在对生命周期函数的第二次调用中静默日志。 However, it may cause undesired behavior in certain cases where a workaround can be used.但是,在某些可以使用变通方法的情况下,它可能会导致不良行为。

As a temporary workaround you can just do let log = console.log at the top level and then it'll always log.作为一种临时解决方法,您可以在顶层执行let log = console.log ,然后它将始终记录。

Got this workaround from - https://github.com/facebook/react/issues/20090从 - https://github.com/facebook/react/issues/20090得到这个解决方法

Here is a code sandbox where you can see the expected output:-这是一个代码沙箱,您可以在其中看到预期的 output:-

编辑严格模式反应日志

The logging inside the then callback of the promise works because it's an async behavior outside the lifecycle methods of React. promise 的then回调中的日志记录有效,因为它是 React生命周期方法之外的异步行为。 React doesn't silence those logs in Strict Mode . React 不会在Strict Mode中使这些日志静音。 But the console.log("inside a Promise obj") is a synchronous task happening inside the Promise executor function which happens during the execution of your component's function body . But the console.log("inside a Promise obj") is a synchronous task happening inside the Promise executor function which happens during the execution of your component's function body .

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM