JavaScript/react:修订间差异
来自康健生活
| (未显示同一用户的51个中间版本) | |||
| 第1行: | 第1行: | ||
== 浅言 == | |||
[[/项目结构设计|项目结构设计]] | |||
== 脚手架 == | |||
* [https://github.com/alan2207/bulletproof-react Bulletproof React] | |||
== 引言 == | == 引言 == | ||
=== 可维护性 === | |||
=== 可重用性 === | |||
=== polymorphism === | |||
=== 感觉 === | |||
应该培养一种感觉。 | |||
== 渲染 == | |||
=== CSR(客户端渲染) === | |||
=== SSR(服务器端渲染) === | |||
== Fiber == | |||
== 方法 == | == 方法 == | ||
{| class="wikitable" | {| class="wikitable" | ||
| 第5行: | 第23行: | ||
| [[/React.FC|React.FC]] || [[/useEffect|useEffect]] || [[/React.StrictMode|React.StrictMode]] | | [[/React.FC|React.FC]] || [[/useEffect|useEffect]] || [[/React.StrictMode|React.StrictMode]] | ||
|- | |- | ||
| [[/useRef|useRef]] || | | [[/useRef|useRef]] || React.lazy || 示例 | ||
|- | |- | ||
| 示例 || 示例 || 示例 | | 示例 || 示例 || 示例 | ||
| 第13行: | 第31行: | ||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
| [[/ref|ref]] || [[/路由|路由]] || | | [[/ref|ref]] || [[/路由|路由]] ||[[/store|store]] | ||
|- | |- | ||
| | | [[/条件渲染|条件渲染]] || 示例 || 示例 | ||
|- | |- | ||
| 示例 || 示例 || 示例 | | 示例 || 示例 || 示例 | ||
| 第44行: | 第62行: | ||
| 示例 || 示例 || 示例 | | 示例 || 示例 || 示例 | ||
|} | |} | ||
== | == 组件 == | ||
=== 特征 === | |||
{| class="wikitable" | |||
|- | |||
| 可组合 Composeable || 可重用 Reusable || 可维护 Maintainable | |||
|} | |||
=== 颗粒 === | |||
{| class="wikitable" | |||
|- | |||
| 原子组件 || 分子组件 || 生物组件 || 模版组件 | |||
|} | |||
=== 函数 === | |||
{| class="wikitable" | |||
|- | |||
| [[/函数式声明组件|函数式声明组件]] | |||
|} | |||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
| [[/modal|modal]] || 示例 || 示例 | | [[/modal|modal]] || 示例 || 示例 | ||
|- | |||
| 示例 || 示例 || 示例 | |||
|- | |||
| 示例 || 示例 || 示例 | |||
|} | |||
{| class="wikitable" | |||
|- | |||
| [[/shouldComponentUpdate|shouldComponentUpdate]] || [[/pureComponent|pureComponent]] | |||
|} | |||
=== 组件模式 === | |||
==== 函数式组件 ==== | |||
==== 受控组件 ==== | |||
==== 非受控组件 ==== | |||
==== 纯组件(PureComponent) ==== | |||
==== 非纯组件 ==== | |||
==== 无状态组件 ==== | |||
==== 复合组件(composite components)模式 ==== | |||
==== 容器组件(smart) ==== | |||
==== 木偶组件(dumb) ==== | |||
==== 高级组件(Higher Order Components) ==== | |||
== bundling == | |||
=== 懒加载(lazy load) === | |||
=== 代码拆分(code splitting) === | |||
== 错误 == | |||
=== 错误边界(ErrorBoundary) === | |||
=== react-error-boundary === | |||
== 状态管理方案 == | |||
{| class="wikitable" | |||
|- | |||
| redux + hooks + redux toolkit || MobX || React-immut || Recoil || Context + hooks || XState || Jotai | |||
|} | |||
== 服务端缓存状态 == | |||
{| class="wikitable" | |||
|- | |||
| react-query + rest + graphql || swr + rest + graphql || apollo client + graphql || urql + grapql | |||
|} | |||
== 表单状态管理 == | |||
{| class="wikitable" | |||
|- | |||
| react hook form || formik || react final form | |||
|} | |||
== URL 状态管理 == | |||
{| class="wikitable" | |||
|- | |||
| react-router-dom | |||
|} | |||
== 生命周期 == | |||
=== 三个状态 === | |||
{| class="wikitable" | |||
|- | |||
| Mounting || 已插入真实 dom | |||
|- | |||
| Updating || 正在被重新渲染 | |||
|- | |||
| Unounting || 已移出真实 dom | |||
|} | |||
=== 五个函数 === | |||
{| class="wikitable" | |||
|- | |||
| componentWillMount || 示例 | |||
|- | |||
| componentDidMount || 示例 | |||
|- | |||
| componentWillUpdate || 示例 | |||
|- | |||
| componentDidUpdate || 示例 | |||
|- | |||
| componentWillUnmount || 示例 | |||
|} | |||
特殊状态处理函数 | |||
{| class="wikitable" | |||
|- | |||
| componentWillReceiveProps || 示例 | |||
|- | |||
| shouldComponentUpdate || 示例 | |||
|} | |||
== 函数 == | |||
{| class="wikitable" | |||
|- | |||
| [[/dangerouslySetInnerHTML|dangerouslySetInnerHTML]] || 示例 || 示例 | |||
|- | |||
| 示例 || 示例 || 示例 | |||
|- | |||
| 示例 || 示例 || 示例 | |||
|} | |||
== 高阶函数 == | |||
{| class="wikitable" | |||
|- | |||
| [[/高阶函数|高阶函数]] || 示例 || 示例 | |||
|- | |- | ||
| 示例 || 示例 || 示例 | | 示例 || 示例 || 示例 | ||
| 第82行: | 第225行: | ||
| 示例 || 示例 || 示例 | | 示例 || 示例 || 示例 | ||
|} | |} | ||
== CSS == | |||
[[/background|background]] | |||
== hooks == | |||
{| class="wikitable" | |||
|- | |||
| [[/useCallback|useCallback]] || [[/useState|useState]] || [[/useRef|useRef]] || [[/useTracked|useTracked]] | |||
|- | |||
| [[/useContext|useContext]] || [[/useReducer|useReducer]] || [[/useMemo|useMemo]] || [[/useImperativeHandle|useImperativeHandle]] | |||
|- | |||
| [[/useEffect|useEffect]] || [[/useLayoutEffect|useLayoutEffect]] || [[/useSelector|useSelector]] || | |||
|} | |||
=== React hooks utils 库 === | |||
# [https://github.com/streamich/react-use react-use] | |||
# [https://ahooks.js.org/zh-CN/guide/ ahooks] | |||
== 颗粒 == | |||
=== 基础颗粒 === | |||
=== 场景颗粒 === | |||
== 算法 == | |||
{| class="wikitable" | |||
|- | |||
| [[/reconciliation|reconciliation]] || 示例 || 示例 | |||
|- | |||
| 示例 || 示例 || 示例 | |||
|- | |||
| 示例 || 示例 || 示例 | |||
|} | |||
== reportWebVitals == | |||
=== 关键指标 === | |||
# CLS (Cumulative Layout Shift) - 累计布局偏移([0-1])。网页布局在加载期间的偏移量,0表示没有偏移,1表示最大偏移。 | |||
# FID (First Input Delay) - 首次输入延迟。首次与网页产生交互动作到浏览器响应此次互动的时间。 | |||
# LCP (Largest Contentful Paint) - 最大内容渲染时间。请求网址到窗口中渲染最大可见内容所需要的时间。 | |||
=== 辅助指标 === | |||
# FCP (First Contentful Paint) - 首次内容绘制。标记浏览器渲染 DOM 第一位内容元素的时间节点,内容可能是文本、图像等元素等。 | |||
# TTFB (Time to First Byte) - 首字节的时间节点。 | |||
== 其他 == | |||
{| class="wikitable" | |||
|- | |||
| [[/自定义参数|自定义参数]] || 示例 || 示例 | |||
|- | |||
| 示例 || 示例 || 示例 | |||
|- | |||
| 示例 || 示例 || 示例 | |||
|} | |||
=== 参考 === | === 参考 === | ||
* https://reactjs.org/docs/events.html#mouse-events | * https://reactjs.org/docs/events.html#mouse-events | ||
| 第99行: | 第289行: | ||
== error == | == error == | ||
* Render methods should be a pure function of props and state; triggering nested component updates from render is not allowed. If necessary, trigger nested updates in componentDidUpdate. | * Render methods should be a pure function of props and state; triggering nested component updates from render is not allowed. If necessary, trigger nested updates in componentDidUpdate. | ||
* Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanu. | |||
* Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function. | |||
* react-dom.development.js:7601 Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node. | |||
: <syntaxhighlight lang="javascript"> | |||
react-dom.development.js:7601 Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node. | |||
at removeChild (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:7601:18) | |||
at unmountHostComponents (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:20449:9) | |||
at commitDeletion (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:20500:5) | |||
at commitMutationEffects (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:22782:11) | |||
at HTMLUnknownElement.callCallback (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:188:14) | |||
at Object.invokeGuardedCallbackDev (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:237:16) | |||
at invokeGuardedCallback (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:292:31) | |||
at commitRootImpl (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:22509:9) | |||
at unstable_runWithPriority (webpack:///./node_modules/scheduler/cjs/scheduler.development.js?:653:12) | |||
at runWithPriority$1 (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:11039:10)</syntaxhighlight> | |||
: 抛这个错误的场景是我在做一个列表拖拽排序的功能 | |||
: [[文件:画板拖拽排序 1.gif|400px|画板拖拽排序 1.gif]] | |||
: 经过排查发现关键点是 react 计算 virtul DOM 与 DOM。当我将一个画板从分组 A 移动到分组 B,是通过实际操作 DOM 结构进行,这一步骤之后如果没有更新 data 不会发生问题,但是拖拽完成之后是需要更新 data 结构的,但是,分组和画板列表的渲染是数据驱动视图模型,当重新加载 data 时,virtul DOM 计算与 DOM 的映射关系,分组 A 在准备移除 data 中被移走的画板时,在分组 A 的 DOM 结构中找不到被移动走的画板(virtul DOM 的分组 A 中依然存在被移走的画板,DOM 中已经不存在),所以抛出上述错误。解题思路如下: | |||
: 1. 方案一:完全遵照<code>数据驱动视图</code>,不再直接操作 DOM 而是通过更改 data 中的数据的位置,重新渲染列表。 | |||
: 2. 方案二:在画板从分组 A 移动到分组 B之后,重新加载数据之前,将画板重新插回分组 A,我实际操作了一下,体验很不好,会有一种拖拽的画板回弹和迟钝卡住的感觉。 | |||
: 3. 方案三:不完全遵照<code>数据驱动视图</code>,依然直接操作 DOM。但是每次视图更新只在页面初加载时进行,再加一层 state,不再监听相关 data 的变化,缺陷,如果发生异常,与后端逻辑没有对上,不能直观察觉到问题,有风险。 | |||
: 4. 方案四:不完全遵照<code>数据驱动视图</code>,依然直接操作 DOM。但是在被拖拽移动的画板添加一层被子元素撑开的 DIV,当拖拽时拖走的是画板,保留上一层 DIV,这样当数据更新时分组会移除相关直接子元素(也就是被添加的那层 DIV)。 | |||
2024年9月14日 (六) 12:05的最新版本
浅言
脚手架
引言
可维护性
可重用性
polymorphism
感觉
应该培养一种感觉。
渲染
CSR(客户端渲染)
SSR(服务器端渲染)
Fiber
方法
| React.FC | useEffect | React.StrictMode |
| useRef | React.lazy | 示例 |
| 示例 | 示例 | 示例 |
关键字
| ref | 路由 | store |
| 条件渲染 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
脚手架
| umi | 示例 | 示例 |
结构
| 数组 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
Component
| displayName | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
组件
特征
| 可组合 Composeable | 可重用 Reusable | 可维护 Maintainable |
颗粒
| 原子组件 | 分子组件 | 生物组件 | 模版组件 |
函数
| 函数式声明组件 |
| modal | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| shouldComponentUpdate | pureComponent |
组件模式
函数式组件
受控组件
非受控组件
纯组件(PureComponent)
非纯组件
无状态组件
复合组件(composite components)模式
容器组件(smart)
木偶组件(dumb)
高级组件(Higher Order Components)
bundling
懒加载(lazy load)
代码拆分(code splitting)
错误
错误边界(ErrorBoundary)
react-error-boundary
状态管理方案
| redux + hooks + redux toolkit | MobX | React-immut | Recoil | Context + hooks | XState | Jotai |
服务端缓存状态
| react-query + rest + graphql | swr + rest + graphql | apollo client + graphql | urql + grapql |
表单状态管理
| react hook form | formik | react final form |
URL 状态管理
| react-router-dom |
生命周期
三个状态
| Mounting | 已插入真实 dom |
| Updating | 正在被重新渲染 |
| Unounting | 已移出真实 dom |
五个函数
| componentWillMount | 示例 |
| componentDidMount | 示例 |
| componentWillUpdate | 示例 |
| componentDidUpdate | 示例 |
| componentWillUnmount | 示例 |
特殊状态处理函数
| componentWillReceiveProps | 示例 |
| shouldComponentUpdate | 示例 |
函数
| dangerouslySetInnerHTML | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
高阶函数
| 高阶函数 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
事件
| React.RefObject | React.EventHandler | React.MouseEvent | React.TouchEvent |
| React.forwardRef | React.FunctionComponent | React.MouseEventHandler | |
| 示例 | 示例 | 示例 |
事件处理
| onChange | onClick | event.persist |
| SyntheticEvent | 示例 | 示例 |
| 示例 | 示例 | 示例 |
事件相关
| 事件冒泡 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
CSS
hooks
| useCallback | useState | useRef | useTracked |
| useContext | useReducer | useMemo | useImperativeHandle |
| useEffect | useLayoutEffect | useSelector |
React hooks utils 库
颗粒
基础颗粒
场景颗粒
算法
| reconciliation | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
reportWebVitals
关键指标
- CLS (Cumulative Layout Shift) - 累计布局偏移([0-1])。网页布局在加载期间的偏移量,0表示没有偏移,1表示最大偏移。
- FID (First Input Delay) - 首次输入延迟。首次与网页产生交互动作到浏览器响应此次互动的时间。
- LCP (Largest Contentful Paint) - 最大内容渲染时间。请求网址到窗口中渲染最大可见内容所需要的时间。
辅助指标
- FCP (First Contentful Paint) - 首次内容绘制。标记浏览器渲染 DOM 第一位内容元素的时间节点,内容可能是文本、图像等元素等。
- TTFB (Time to First Byte) - 首字节的时间节点。
其他
| 自定义参数 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
参考
- https://reactjs.org/docs/events.html#mouse-events
- https://fettblog.eu/typescript-react/events/
- https://docs.microsoft.com/en-us/javascript/api/office-ui-fabric-react/positioningcontainer?view=office-ui-fabric-react-latest
- https://reactjs.org/docs/handling-events.html
其他
| class | 元素显示隐藏 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
error
- Render methods should be a pure function of props and state; triggering nested component updates from render is not allowed. If necessary, trigger nested updates in componentDidUpdate.
- Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanu.
- Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
- react-dom.development.js:7601 Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
react-dom.development.js:7601 Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node. at removeChild (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:7601:18) at unmountHostComponents (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:20449:9) at commitDeletion (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:20500:5) at commitMutationEffects (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:22782:11) at HTMLUnknownElement.callCallback (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:188:14) at Object.invokeGuardedCallbackDev (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:237:16) at invokeGuardedCallback (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:292:31) at commitRootImpl (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:22509:9) at unstable_runWithPriority (webpack:///./node_modules/scheduler/cjs/scheduler.development.js?:653:12) at runWithPriority$1 (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:11039:10)
- 抛这个错误的场景是我在做一个列表拖拽排序的功能

- 经过排查发现关键点是 react 计算 virtul DOM 与 DOM。当我将一个画板从分组 A 移动到分组 B,是通过实际操作 DOM 结构进行,这一步骤之后如果没有更新 data 不会发生问题,但是拖拽完成之后是需要更新 data 结构的,但是,分组和画板列表的渲染是数据驱动视图模型,当重新加载 data 时,virtul DOM 计算与 DOM 的映射关系,分组 A 在准备移除 data 中被移走的画板时,在分组 A 的 DOM 结构中找不到被移动走的画板(virtul DOM 的分组 A 中依然存在被移走的画板,DOM 中已经不存在),所以抛出上述错误。解题思路如下:
- 1. 方案一:完全遵照
数据驱动视图,不再直接操作 DOM 而是通过更改 data 中的数据的位置,重新渲染列表。 - 2. 方案二:在画板从分组 A 移动到分组 B之后,重新加载数据之前,将画板重新插回分组 A,我实际操作了一下,体验很不好,会有一种拖拽的画板回弹和迟钝卡住的感觉。
- 3. 方案三:不完全遵照
数据驱动视图,依然直接操作 DOM。但是每次视图更新只在页面初加载时进行,再加一层 state,不再监听相关 data 的变化,缺陷,如果发生异常,与后端逻辑没有对上,不能直观察觉到问题,有风险。 - 4. 方案四:不完全遵照
数据驱动视图,依然直接操作 DOM。但是在被拖拽移动的画板添加一层被子元素撑开的 DIV,当拖拽时拖走的是画板,保留上一层 DIV,这样当数据更新时分组会移除相关直接子元素(也就是被添加的那层 DIV)。
No categories