React: 16 - 18 - 19

Links

人人都能看懂的 React : https://learnreact.design/posts/what-is-react/zh

React 周邊

https://github.com/nfl/react-helmet 插入原生 head,比如 meta

React 16 -> 18(并發(fā))

React 中的事件機(jī)制. 批量渲染,不能跨事件,onClick,addEventListener, Concurrent Mode,Sync Render

React 19 English Blog

https://react.dev/blog/2024/04/25/react-19

Tips

React 19 存在新的呢? useActionState, useFormStatus, useOptimistic, use
React 19 存在升級呢? <ThemeContext; ref clean; useDeferredValue; initialValue; 文檔元數(shù)據(jù) 手動加載樣式表 支持異步腳本 資源預(yù)加載 錯誤處理(createRoot 存在三個錯誤處理選項)
React 19 我好奇的? React Compiler

Keywords

  • JSX Transform

安裝

{
  "dependencies": {
    "@types/react": "npm:types-react@beta",
    "@types/react-dom": "npm:types-react-dom@beta"
  },
  "overrides": {
    "@types/react": "npm:types-react@beta",
    "@types/react-dom": "npm:types-react-dom@beta"
  }
}

破壞性變更

  • Error in render 不會拋出兩次了
  • 移除廢棄的 API
    • ReactDOM.render was deprecated in March 2022 (v18.0.0)
    • 19 Deprecated: element.ref
    • 19 Deprecated: react-test-renderer

值得關(guān)注的

  • 支持ESM - based CDN
  <script type="module">
    import React from "https://esm.sh/react@19/?dev"
    import ReactDOMClient from "https://esm.sh/react-dom@19/client?dev"
  </script>
  • Ref cleanups required
  • useRef requires an argument
    之前會遇到以下這種情況,但是現(xiàn)在不會了
const ref = useRef < number > null;
// Can not assign to 'current' because it is a read-only property
ref.current = 1;
  • The JSX namespace in TypeScript (scoped jsx)
  declare module "react"

New Features

actions

useTransition ; useOptimistic; use

const [isPending, startTransition] = useTransition( );

use API

  • use hook is preferred over useContext hook, because it is more flexible.
const theme = use(ThemeContext);

ref as a prop

deprecate forwardRef

<Context /> as a provider

<ThemeContext value="dark"> {children} </ThemeContext>

Cleanup functions for refs

類似useEffect的使用

ref => {
  // ref created
  ...
  // NEW: return a cleanup function to reset
  // The ref when element is removed from DOM.
  return () => {
    // ref cleanup
  };
};

useDeferredValue

類比防抖節(jié)流函數(shù), 只不過useDeferredValue更適合優(yōu)化渲染
是一個體驗優(yōu)化,可以延遲渲染UI的某一個部分,也就是里面包裹的是state
官方例子: searchBar + list: 可以將 list 的 result 使用useDeferredValue包裹一下

Additional

  • If you have a lot of unsound access to xxx.
  • Some of the removed have types been moved to more relevant packages.
  • You will make an API request, and then handle the response. Then, we have to handle pending states, errors,optimistic updates, and sequential requests.
  • The main difference between useDeferredValue and unoptimized re-rendering
  • Let’s walk through an example to see when this is useful.

Magic

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容