useSyncExternalStore 的应用
我们是袋鼠云数栈 UED 团队,致力于打造优秀的一站式数据中台产品。我们始终保持工匠精神,探索前端道路,为社区积累并传播经验价值。
本文作者:修能
学而不思则罔,思而不学则殆 。 --- 《论语·为政》
What
useSyncExternalStoreis a React Hook that lets you subscribe to an external store.
useSyncExternalStore 是一个支持让用户订阅外部存储的 Hook。官方文档
const snapshot = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot?)
Why
首先,我们这里基于 molecule1.x 的版本抽象了一个简易版的 mini-molecule。
import { EventBus } from "../utils";
type Item = { key: string };
// 声明一个事件订阅
const eventBus = new EventBus();
// 声明模块数据类型
class Model {
constructor(public data: Item[] = [], public current?: string) {}
}
export class Service {
protected state: Model;
constructor() {
this.state = new Model();
}
setState(nextState: Partial<Model>) {
this.state = { ...this.state, ...nextState };
this.render(this.state);
}
private render(state: Model) {
eventBus.emit("render", state);
}
}
export default function Home() {
const state = useExternal();
if (!state) return <div>loading...</div>;
return (
<>
<strong>{state.current || "empty"}</strong>
<ul>
{state.data.map((i) => (
<li key={i.key}>{i.key}</li>
))}
</ul>
</>
);
}
const service = new Service();
function useExternal() {
const [state, setState] = useState<Model | undefined>(undefined);
useEffect(() => {
setState(service.getState());
service.onUpdateState((next) => {
setState(next);
});
}, []);
return state;
}
如上面代码所示,已经实现了从外部存储获取相关数据,并且监听外部数据的更新,并触发函数组件的更新。
接下来实现更新外部数据的操作。
export default function Home() {
const state = useExternal();
if (!state) return <div>loading...</div>;
return (
<>
<ul>
{state.data.map((i) => (
<li key={i.key}>{i.key}</li>
))}
</ul>
+ <button onClick={() => service.insert(`${new Date().valueOf()}`)}>
+ add list
+ </button>
</>
);
}
其实要做的比较简单,就是增加了一个触发的按钮去修改数据即可。
上述这种比较简单的场景下所支持的 useExternal 写起来也是比较简单的。当你的场景越发复杂,你所需要考虑的就越多。就会导致项目的复杂度越来越高。而此时,如果有一个官方出品,有 React 团队做背书的 API 则会舒服很多。
以下是 useSyncExternlaStore 的 shim 版本相关代码:
function useSyncExternalStore(subscribe, getSnapshot, // Note: The shim does not use getServerSnapshot, because pre-18 versions of
// React do not expose a way to check if we're hydrating. So users of the shim
// will need to track that themselves and return the correct value
// from `getSnapshot`.
getServerSnapshot) {
{
if (!didWarnOld18Alpha) {
if (React.startTransition !== undefined) {
didWarnOld18Alpha = true;
error('You are using an outdated, pre-release alpha of React 18 that ' + 'does not support useSyncExternalStore. The ' + 'use-sync-external-store shim will not work correctly. Upgrade ' + 'to a newer pre-release.');
}
}
} // Read the current snapshot from the store on every render. Again, this
// breaks the rules of React, and only works here because of specific
// implementation details, most importantly that updates are
// always synchronous.
var value = getSnapshot();
{
if (!didWarnUncachedGetSnapshot) {
var cachedValue = getSnapshot();
if (!objectIs(value, cachedValue)) {
error('The result of getSnapshot should be cached to avoid an infinite loop');
didWarnUncachedGetSnapshot = true;
}
}
} // Because updates are synchronous, we don't queue them. Instead we force a
// re-render whenever the subscribed state changes by updating an some
// arbitrary useState hook. Then, during render, we call getSnapshot to read
// the current value.
//
// Because we don't actually use the state returned by the useState hook, we
// can save a bit of memory by storing other stuff in that slot.
//
// To implement the early bailout, we need to track some things on a mutable
// object. Usually, we would put that in a useRef hook, but we can stash it in
// our useState hook instead.
//
// To force a re-render, we call forceUpdate({inst}). That works because the
// new object always fails an equality check.
var _useState = useState({
inst: {
value: value,
getSnapshot: getSnapshot
}
}),
inst = _useState[0].inst,
forceUpdate = _useState[1]; // Track the latest getSnapshot function with a ref. This needs to be updated
// in the layout phase so we can access it during the tearing check that
// happens on subscribe.
useLayoutEffect(function () {
inst.value = value;
inst.getSnapshot = getSnapshot; // Whenever getSnapshot or subscribe changes, we need to check in the
// commit phase if there was an interleaved mutation. In concurrent mode
// this can happen all the time, but even in synchronous mode, an earlier
// effect may have mutated the store.
if (checkIfSnapshotChanged(inst)) {
// Force a re-render.
forceUpdate({
inst: inst
});
}
}, [subscribe, value, getSnapshot]);
useEffect(function () {
// Check for changes right before subscribing. Subsequent changes will be
// detected in the subscription handler.
if (checkIfSnapshotChanged(inst)) {
// Force a re-render.
forceUpdate({
inst: inst
});
}
var handleStoreChange = function () {
// TODO: Because there is no cross-renderer API for batching updates, it's
// up to the consumer of this library to wrap their subscription event
// with unstable_batchedUpdates. Should we try to detect when this isn't
// the case and print a warning in development?
// The store changed. Check if the snapshot changed since the last time we
// read from the store.
if (checkIfSnapshotChanged(inst)) {
// Force a re-render.
forceUpdate({
inst: inst
});
}
}; // Subscribe to the store and return a clean-up function.
return subscribe(handleStoreChange);
}, [subscribe]);
useDebugValue(value);
return value;
}
How
针对上述例子进行改造
const service = new Service();
export default function Home() {
const state = useSyncExternalStore(
(cb) => () => service.onUpdateState(cb),
service.getState.bind(service)
);
if (!state) return <div>loading...</div>;
return (
<>
<ul>
{state.data.map((i) => (
<li key={i.key}>{i.key}</li>
))}
</ul>
<button onClick={() => service.insert(`${new Date().valueOf()}`)}>
add list
</button>
</>
);
}
在 Molecule 中使用
import { useContext, useMemo } from 'react';
import type { IMoleculeContext } from 'mo/types';
import { useSyncExternalStore } from 'use-sync-external-store/shim';
import { Context } from '../context';
type Selector = keyof IMoleculeContext;
type StateType<T extends keyof IMoleculeContext> = ReturnType<IMoleculeContext[T]['getState']>;
export default function useConnector<T extends Selector>(selector: T) {
const { molecule } = useContext(Context);
const target = useMemo(() => molecule[selector], [molecule]);
const subscribe = useMemo(() => {
return (notify: () => void) => {
target.onUpdateState(notify);
return () => target.removeOnUpdateState(notify);
};
}, []);
return useSyncExternalStore(subscribe, () => target.getState()) as StateType<T>;
}
最后
欢迎关注【袋鼠云数栈UED团队】~
袋鼠云数栈 UED 团队持续为广大开发者分享技术成果,相继参与开源了欢迎 star
- 大数据分布式任务调度系统——Taier
- 轻量级的 Web IDE UI 框架——Molecule
- 针对大数据领域的 SQL Parser 项目——dt-sql-parser
- 袋鼠云数栈前端团队代码评审工程实践文档——code-review-practices
- 一个速度更快、配置更灵活、使用更简单的模块打包器——ko
- 一个针对 antd 的组件测试工具库——ant-design-testing
useSyncExternalStore 的应用的更多相关文章
- 我的 React 最佳实践
There are a thousand Hamlets in a thousand people's eyes. ----- 威廉·莎士比亚 免责声明:以下充满个人观点,辩证学习 React 目前开 ...
随机推荐
- [转]OpenCV三角测量重建triangulatePoints原理解析
opencv源代码注释 附上opencv三角测量函数的主要代码和注释 cvTriangulatePoints(CvMat* projMatr1, CvMat* projMatr2, CvMat* pr ...
- WxPython跨平台开发框架之模块字段权限的管理
在我的很多Winform开发项目中,统一采用了权限管理模块来进行各种权限的控制,包括常规的功能权限(工具栏.按钮.菜单权限),另外还可以进行字段级别的字段权限控制,字段权限是我们在一些对权限要求比较严 ...
- G1原理—1.G1回收器的分区机制
大纲 1.G1垃圾回收器的分区(Region大小+G1分区+Region过大过小和计算) 2.Region大小的计算原理(先转字节然后确定2的n次幂再通过1左移n位) 3.新生代分区及自动扩展(新生代 ...
- Python 潮流周刊#85:让 AI 帮你写出更好的代码(摘要)
本周刊由 Python猫 出品,精心筛选国内外的 250+ 信息源,为你挑选最值得分享的文章.教程.开源项目.软件工具.播客和视频.热门话题等内容.愿景:帮助所有读者精进 Python 技术,并增长职 ...
- c# 无法捕获 System.NullReferenceException?
..在VS debug 模式下会"无法捕获" System.NullReferenceException......但是在release 或者外部运行是能捕获的. test cod ...
- ForkJoin全解2:forkjoin实际工作流程与实现
1.相关概念解释 1.1 "内部"和外部 当一个操作是在非ForkjoinThread的线程中进行的,则称该操作为外部操作.比如我们前面执行pool.invoke,invoke内又 ...
- python的类机制
python的类机制 参考:python面向对象 概念 方法重写/覆盖:若从父类继承的方法不能满足子类的需求,可以对其进行改写. 类变量:在实例化对象中是公用的,定义在类中,且在函数体之外,通常不作为 ...
- MySQL中联合主键的 in 查询
就一句话: SELECT * from Projects WHERE (Prj,SubID) in (SELECT Prj,SubID FROM SP where stage='设计' and 负责人 ...
- not in 和 not exists 比较和用法
尽量不要使用not in(它会调用子查询),而尽量使用not exists(它会调用关联子查询).查询语句使用了not in,那么对内外表都进行全表扫描,没有用到索引:而not exists的子查询依 ...
- Web安全知识记录
本文分享自天翼云开发者社区<Web安全知识记录>,作者:赵****雅 1.网站置于服务器中,而服务器则是指连接在网络中的一台计算机.当我们浏览网站时,实际上就是我们用个人计算机通过网络访问 ...