登录
注册
开源
企业版
高校版
搜索
帮助中心
使用条款
关于我们
开源
企业版
高校版
私有云
模力方舟
AI 队友
登录
注册
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
取消
前往登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
Watch
不关注
关注所有动态
仅关注版本发行动态
关注但不提醒动态
23
Star
193
Fork
48
yanleweb
/
interview-question
代码
Issues
1091
Pull Requests
0
Wiki
统计
流水线
服务
质量分析
Jenkins for Gitee
腾讯云托管
腾讯云 Serverless
悬镜安全
阿里云 SAE
Codeblitz
SBOM
我知道了,不再自动展开
更新失败,请稍后重试!
移除标识
内容风险标识
本任务被
标识为内容中包含有代码安全 Bug 、隐私泄露等敏感信息,仓库外成员不可访问
[React] Recoil 里面 selectorFamily 的作用是什么?和 selector 有啥区别【热度: 200】
待办的
#IBLDHM
yanleweb
拥有者
创建于
2025-02-11 00:23
**关键词**:Recoil selector 和 selectorFamily 在 Recoil 中,`selectorFamily` 和 `selector` 都是用于创建派生状态的工具,但它们在使用场景和功能上存在一些差异,下面为你详细介绍它们的作用以及区别。 ### `selector` 的作用 `selector` 用于创建派生状态,它可以根据一个或多个原子(`atom`)状态计算出新的状态。`selector` 的值会自动进行记忆化,只有当依赖的状态发生变化时才会重新计算。以下是一个简单的 `selector` 示例: ```jsx import { atom, selector, useRecoilValue } from "recoil"; // 定义一个原子状态 const textState = atom({ key: "textState", default: "Hello, Recoil!", }); // 定义一个 selector,它依赖于 textState const textLengthState = selector({ key: "textLengthState", get: ({ get }) => { const text = get(textState); return text.length; }, }); const App = () => { const textLength = useRecoilValue(textLengthState); return ( <div> <p>Text length: {textLength}</p> </div> ); }; ``` 在这个例子中,`textLengthState` 是一个 `selector`,它根据 `textState` 的值计算文本的长度。 ### `selectorFamily` 的作用 `selectorFamily` 是 `selector` 的一种扩展,它允许你创建一系列相关的 `selector`,这些 `selector` 可以根据传入的参数动态生成。这在需要根据不同的输入生成不同的派生状态时非常有用,比如根据不同的 ID 获取不同的数据。以下是一个 `selectorFamily` 的示例: ```jsx import { atom, selectorFamily, useRecoilValue } from "recoil"; // 模拟一个数据集合 const dataState = atom({ key: "dataState", default: { 1: { name: "Item 1" }, 2: { name: "Item 2" }, 3: { name: "Item 3" }, }, }); // 定义一个 selectorFamily const itemSelectorFamily = selectorFamily({ key: "itemSelectorFamily", get: (itemId) => ({ get }) => { const data = get(dataState); return data[itemId]; }, }); const App = () => { const item1 = useRecoilValue(itemSelectorFamily(1)); const item2 = useRecoilValue(itemSelectorFamily(2)); return ( <div> <p>Item 1: {item1?.name}</p> <p>Item 2: {item2?.name}</p> </div> ); }; ``` 在这个例子中,`itemSelectorFamily` 是一个 `selectorFamily`,它根据传入的 `itemId` 从 `dataState` 中获取相应的数据。通过不同的 `itemId` 可以获取不同的派生状态。 ### 两者的区别 - **参数化能力** - **`selector`**:没有参数化的能力,它的依赖和计算逻辑是固定的,每次使用时都会计算相同的派生状态。 - **`selectorFamily`**:支持参数化,可以根据传入的不同参数生成不同的 `selector` 实例,从而计算出不同的派生状态。 - **使用场景** - **`selector`**:适用于派生状态的计算逻辑固定,不依赖外部参数的场景,比如根据一个固定的原子状态计算其长度、总和等。 - **`selectorFamily`**:适用于需要根据不同的输入动态生成派生状态的场景,比如根据不同的 ID 获取不同的数据、根据不同的筛选条件获取过滤后的数据等。 - **记忆化机制** - **`selector`**:对整个 `selector` 进行记忆化,只要依赖的状态不变,就不会重新计算。 - **`selectorFamily`**:对每个根据不同参数生成的 `selector` 实例进行记忆化,不同参数对应的实例之间相互独立,每个实例的计算结果会分别进行记忆化。 综上所述,`selectorFamily` 是 `selector` 的增强版本,当你需要根据不同的参数动态生成派生状态时,应该使用 `selectorFamily`;而当派生状态的计算逻辑固定时,使用 `selector` 即可。
**关键词**:Recoil selector 和 selectorFamily 在 Recoil 中,`selectorFamily` 和 `selector` 都是用于创建派生状态的工具,但它们在使用场景和功能上存在一些差异,下面为你详细介绍它们的作用以及区别。 ### `selector` 的作用 `selector` 用于创建派生状态,它可以根据一个或多个原子(`atom`)状态计算出新的状态。`selector` 的值会自动进行记忆化,只有当依赖的状态发生变化时才会重新计算。以下是一个简单的 `selector` 示例: ```jsx import { atom, selector, useRecoilValue } from "recoil"; // 定义一个原子状态 const textState = atom({ key: "textState", default: "Hello, Recoil!", }); // 定义一个 selector,它依赖于 textState const textLengthState = selector({ key: "textLengthState", get: ({ get }) => { const text = get(textState); return text.length; }, }); const App = () => { const textLength = useRecoilValue(textLengthState); return ( <div> <p>Text length: {textLength}</p> </div> ); }; ``` 在这个例子中,`textLengthState` 是一个 `selector`,它根据 `textState` 的值计算文本的长度。 ### `selectorFamily` 的作用 `selectorFamily` 是 `selector` 的一种扩展,它允许你创建一系列相关的 `selector`,这些 `selector` 可以根据传入的参数动态生成。这在需要根据不同的输入生成不同的派生状态时非常有用,比如根据不同的 ID 获取不同的数据。以下是一个 `selectorFamily` 的示例: ```jsx import { atom, selectorFamily, useRecoilValue } from "recoil"; // 模拟一个数据集合 const dataState = atom({ key: "dataState", default: { 1: { name: "Item 1" }, 2: { name: "Item 2" }, 3: { name: "Item 3" }, }, }); // 定义一个 selectorFamily const itemSelectorFamily = selectorFamily({ key: "itemSelectorFamily", get: (itemId) => ({ get }) => { const data = get(dataState); return data[itemId]; }, }); const App = () => { const item1 = useRecoilValue(itemSelectorFamily(1)); const item2 = useRecoilValue(itemSelectorFamily(2)); return ( <div> <p>Item 1: {item1?.name}</p> <p>Item 2: {item2?.name}</p> </div> ); }; ``` 在这个例子中,`itemSelectorFamily` 是一个 `selectorFamily`,它根据传入的 `itemId` 从 `dataState` 中获取相应的数据。通过不同的 `itemId` 可以获取不同的派生状态。 ### 两者的区别 - **参数化能力** - **`selector`**:没有参数化的能力,它的依赖和计算逻辑是固定的,每次使用时都会计算相同的派生状态。 - **`selectorFamily`**:支持参数化,可以根据传入的不同参数生成不同的 `selector` 实例,从而计算出不同的派生状态。 - **使用场景** - **`selector`**:适用于派生状态的计算逻辑固定,不依赖外部参数的场景,比如根据一个固定的原子状态计算其长度、总和等。 - **`selectorFamily`**:适用于需要根据不同的输入动态生成派生状态的场景,比如根据不同的 ID 获取不同的数据、根据不同的筛选条件获取过滤后的数据等。 - **记忆化机制** - **`selector`**:对整个 `selector` 进行记忆化,只要依赖的状态不变,就不会重新计算。 - **`selectorFamily`**:对每个根据不同参数生成的 `selector` 实例进行记忆化,不同参数对应的实例之间相互独立,每个实例的计算结果会分别进行记忆化。 综上所述,`selectorFamily` 是 `selector` 的增强版本,当你需要根据不同的参数动态生成派生状态时,应该使用 `selectorFamily`;而当派生状态的计算逻辑固定时,使用 `selector` 即可。
评论 (
0
)
登录
后才可以发表评论
状态
待办的
待办的
进行中
已完成
已关闭
负责人
未设置
标签
web框架
未设置
标签管理
里程碑
高
未关联里程碑
Pull Requests
未关联
未关联
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
未关联
分支 (1)
标签 (64)
master
0.0.76
0.0.75
0.0.74
0.0.73
0.0.72
0.0.71
0.0.70
0.0.69
0.0.68
0.0.67
0.0.66
0.0.65
0.0.64
0.0.63
0.0.62
0.0.61
0.0.60
0.0.59
0.0.58
0.0.57
0.0.56
0.0.55
0.0.54
0.0.53
0.0.52
0.0.51
0.0.50
0.0.49
0.0.48
0.0.47
0.0.46
0.0.45
0.0.44
0.0.43
0.0.42
0.0.41
0.0.40
0.0.39
0.0.38
0.0.37
0.0.36
0.0.35
0.0.34
0.0.33
0.0.32
0.0.31
0.0.30
0.0.29
0.0.28
0.0.27
0.0.26
0.0.25
0.0.24
0.0.23
0.0.22
0.0.21
0.0.20
0.0.19
0.0.18
0.0.17
0.0.16
0.0.15
0.0.14
0.0.13
开始日期   -   截止日期
-
置顶选项
不置顶
置顶等级:高
置顶等级:中
置顶等级:低
优先级
不指定
严重
主要
次要
不重要
参与者(1)
TypeScript
1
https://gitee.com/yanleweb/interview-question.git
git@gitee.com:yanleweb/interview-question.git
yanleweb
interview-question
interview-question
点此查找更多帮助
搜索帮助
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册