首页 > web前端 > js教程 > 正文

useSyncExternalStoreExports 状态源码解释

王林
发布: 2024-09-11 09:31:40
转载
966人浏览过

在本文中,我们将了解 zustand 如何在其[源代码]中使用 usesyncexternalstoreexports。

useSyncExternalStoreExports 状态源码解释

usesyncexternalstoreexports 是从 use-sync-external-store/shim/with-selector 导入的。 use-sync-external-store 是 react.usesyncexternalstore 的向后兼容垫片,可与任何支持 hooks 的 react 配合使用。

读到上面这句话,你可能想知道什么是usesyncexternalstore。

usesyncexternalstore

usesyncexternalstore 是一个 react hook,可让您订阅外部存储。

const snapshot = usesyncexternalstore(subscribe, getsnapshot, getserversnapshot?)
登录后复制

使用 usesyncexternalstore 从外部存储读取一个值,该值可以是:

  1. 在 react 之外保存状态的第三方状态管理库。

  2. 公开可变值和事件以订阅其更改的浏览器 api。

用法示例:

import { usesyncexternalstore } from 'react';
import { todosstore } from './todostore.js';

function todosapp() {
  const todos = usesyncexternalstore(todosstore.subscribe, todosstore.getsnapshot);
  // ...
}
登录后复制

上面的例子摘自react文档。

usesyncexternalstore 在 zustand 中的用法:

zustand 在 src/traditional.ts 中使用 usesyncexternalstore。

import reactexports from 'react'
// eslint-disable-next-line import/extensions
import usesyncexternalstoreexports from 'use-sync-external-store/shim/with-selector'
import { createstore } from './vanilla.ts'
import type {
  mutate,
  statecreator,
  storeapi,
  storemutatoridentifier,
} from './vanilla.ts'

const { usedebugvalue } = reactexports
const { usesyncexternalstorewithselector } = usesyncexternalstoreexports
登录后复制

usesyncexternalstorewithselector 是从 usesyncexternalstoreexports 解构而来,并在 usestorewithequalityfn 中使用。

export function useStoreWithEqualityFn<TState, StateSlice>(
  api: ReadonlyStoreApi<TState>,
  selector: (state: TState) => StateSlice = identity as any,
  equalityFn?: (a: StateSlice, b: StateSlice) => boolean,
) {
  const slice = useSyncExternalStoreWithSelector(
    api.subscribe,
    api.getState,
    api.getInitialState,
    selector,
    equalityFn,
  )
  useDebugValue(slice)
  return slice
}
登录后复制

usesyncexternalstorewithselector 有 api.subscribe、api.getstate、api.getinitialstate、selector 和 equalfn。

关于我们:

在 think throo,我们的使命是教授受开源项目启发的最佳实践。

通过在 next.js/react 中练习高级架构概念,将您的编码技能提高 10 倍,学习最佳实践并构建生产级项目。

我们是开源的 — https://github.com/thinkthroo/thinkthroo (请给我们一颗星!)

通过我们基于代码库架构的高级课程来提高您的团队的技能。请通过 hello@thinkthroo.com 联系我们以了解更多信息!

参考资料:

  1. https://github.com/pmndrs/zustand/blob/main/src/traditional.ts#l44

  2. https://www.npmjs.com/package/use-sync-external-store

  3. https://legacy.reactjs.org/docs/hooks-reference.html#usesyncexternalstore

  4. https://react.dev/reference/react/usesyncexternalstore

  5. https://github.com/reactwg/react-18/discussions/86



以上就是useSyncExternalStoreExports 状态源码解释的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号