跳至正文
Wener Notes

React Version

约 2 分钟阅读
标签:版本
verdatenotes
React 19RSC, async, WebComponents
React 182022-03-29ReactDOM.createRoot
React 172020-10-20
React 16.82019-02-06Hooks

Roadmap

  • Add a "module" entry in package.json
    • React 作为 ESM
    • 目前主要问题是 cjs 和 esm 混合可能导致各种问题 - react 不能轻易变动
    • 目前大的生态在放弃 cjs - 还是会保留 umd
  • Formalize top-level ES exports
    • 解决 import * as React 问题
    • 目前 ESM 内已经使用 import React from 'react'; - 2020-02
  • Support for reparenting
  • Remove Factory Components
    • deprecated in React 17.x

React 19

React 18

  • FC 默认无 children 属性
  • automatic batching
  • concurrent feature
    • startTransition
    • useTransition
    • useDeferredValue
      • 类似 useDebounedValue 但结合 React 并行进行渲染,而不是自行决定 delay 时间
      • 提升 UX 体验
    • APIs for library
  • streaming server renderer
    • suspense on server
      • 细粒度控制 hydration
        • hydration 不会阻塞浏览器,可能会被中断,根据用户操作进行优先
  • 实验特性
    • server component - 可通过 nextjs 使用
    • React.memo - UX vs DX
      • React Forget - 自动 memo 的编译器
  • 参考
  • ReactDOM.render -> ReactDOM.createRoot
text
// before
ReactDOM.render(<App />, el);
// after
const root = ReactDOM.createRoot(el);
root.render(<App />);

// 服务端渲染 为 aria 生成唯一 ID
const MyInputs = () => {
  const id = useId();
  return (
    <>
      <label htmlForm={`${id}-name`}>Name</label>
      <input id={`${id}-name`} />
    </>
  );
};

React 17

  • 内部结构变化,为下一个版本做准备 - 无功能变化
  • children 处理有变化 - 总是作为 props 传递
    • 早期区分是为了在 DEV 时区别静态和动态内容
  • key 独立传递 - jsx('div', props, key)
  • depreacted componentWillMount, componentWillReceiveProps, componentWillUpdate
  • 不再需要对事件进行 persist
text
<input
  onChange={(e) => {
    e.persist(); // 17 之后不再需要
    update((s) => {
      // 之前如果不 persist 可能为 null
      s.value = e.target.value;
    });
  }}
/>
  • 新的 JSX 转换 - jsx 不再需要引入 React
    • react/jsx-runtime
    • react/jsx-dev-runtime
    • 也可以与其他 JSX 项目共享
text
import { jsx as _jsx } from 'react/jsx-runtime';

function App() {
  // return React.createElement('h1', null, 'Hello world');
  return _jsx('h1', { children: 'Hello world' });
}
  • React DOM
    • ReactDOM.render(<App />, rootNode) 会在 rootNode 监听事件 - 之前是 document 上监听

React 16.8

  • 新增 Functional Component
  • 新增 Hooks

关联信息

反向链接、本文链接的其他页面和外部资料。

参考资料

GitHub9 条
Hacker News1 条
其他外链2 条
最近更新commit 4d57414编辑