How could we handle expensive or frequent subscriptions that need to access some changing prop/state with hooks?
未关闭
还没有人认领这个 Issue。
- 主要语言
- JavaScript
- 星标
- 11.8k
- 派生
- 7.9k
- 平均合并
- 1 天 11 小时
- 30 天内合并 PR
- 11
描述
It sort of looks hard without breaking the rule
const Component = ({ outer }) => {
const [mutable, setMutable] = useState(0);
const [cond, setCond] = useState(false);
useEffect(() => {
const handle = doExpensiveSubscription((value) => {
if (cond) {
// cond and outer are stale values
setMutable(calculate(outer, value));
}
});
return handle.unsubscribe;
// the ESLint exhaustive-deps rule would warn about unmet dependency: outer, cond
}, []);
return <div onClick={() => setCond(!cond)}>{mutable}</div>;
};
My temporary solution to the problem looks like
const Component = ({ outer }) => {
const [mutable, setMutable] = useState(0);
const [cond, setCond] = useState(false);
const paramRef = useRef({});
paramRef.current = { outer, cond };
useEffect(() => {
const handle = doExpensiveSubscription((value) => {
const { outer, cond } = paramRef.current;
if (cond) {
// cond and outer hold their respective current values now
setMutable(calculate(outer, value));
}
});
return handle.unsubscribe;
// the ESLint exhaustive-deps rule would warn about unmet dependency: outer, cond
}, []);
return <div onClick={() => setCond(!cond)}>{mutable}</div>;
};
There does not seem to be a specific documentation about that.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 issue 中的 hooks 示例开始,并检查现有的 useEffect、useRef 和 exhaustive-deps 文档。确定文档应如何解释那些需要当前 props 或 state、但无需重新创建高成本 subscription 的订阅;当指南涵盖 stale value 问题和 dependency 警告时即完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, react
- 领域
- documentation
- Issue 类型
- 文档
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100