• React v18.0 How to Upgrade to React 18


    小结:

    1、

    A key property of Concurrent React is that rendering is interruptible. When you first upgrade to React 18, before adding any concurrent features, updates are rendered the same as in previous versions of React — in a single, uninterrupted, synchronous transaction. With synchronous rendering, once an update starts rendering, nothing can interrupt it until the user can see the result on screen.

    In a concurrent render, this is not always the case. React may start rendering an update, pause in the middle, then continue later. It may even abandon an in-progress render altogether. React guarantees that the UI will appear consistent even if a render is interrupted. To do this, it waits to perform DOM mutations until the end, once the entire tree has been evaluated. With this capability, React can prepare new screens in the background without blocking the main thread. This means the UI can respond immediately to user input even if it’s in the middle of a large rendering task, creating a fluid user experience.

    2、

    // Before: only React events were batched.
    setTimeout(() => {
      setCount(c => c + 1);
      setFlag(f => !f);
      // React will render twice, once for each state update (no batching)
    }, 1000);
    
    // After: updates inside of timeouts, promises,
    // native event handlers or any other event are batched.`
    setTimeout(() => {
      setCount(c => c + 1);
      setFlag(f => !f);
      // React will only re-render once at the end (that's batching!)
    }, 1000);

    3、

    For example, when you select a filter in a dropdown, you expect the filter button itself to respond immediately when you click. However, the actual results may transition separately. A small delay would be imperceptible and often expected. And if you change the filter again before the results are done rendering, you only care to see the latest results.

    import {startTransition} from 'react';
    
    // Urgent: Show what was typed
    setInputValue(input);
    
    // Mark any state updates inside as transitions
    startTransition(() => {
      // Transition: Show the results
      setSearchQuery(input);
    });

    React v18.0 – React Blog https://reactjs.org/blog/2022/03/29/react-v18.html

    How to Upgrade to React 18 – React Blog https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html

     Hooks API Reference – React https://reactjs.org/docs/hooks-reference.html#usedeferredvalue

  • 相关阅读:
    CodeForces-786B Legacy (线段树优化建图,单源最短路)
    CodeForces-528C Data Center Drama
    CodeForces-723E One-Way Reform
    2-SAT入门
    POJ-3683 Priest John's Busiest Day (2-SAT 求任意可行方案)
    转载: 8天学通MongoDB——第一天 基础入门
    C# 非EF注册登录与EF注册登录
    Asp.Net入门(三)
    非EF分页
    sql语句错误大集合
  • 原文地址:https://www.cnblogs.com/rsapaper/p/16076893.html
Copyright © 2020-2023  润新知