reactjs / reactjs/react.dev

Amend documentation for skipping useEffect

未关闭
#1,799 2 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
JavaScript
星标
11.8k
派生
7.9k
平均合并
1 天 11 小时
30 天内合并 PR
11

描述

This is a documentation improvement request.

Currently https://reactjs.org/docs/hooks-effect.html#tip-optimizing-performance-by-skipping-effects explains skipping a useEffect based on a state variable by showing the following code:

useEffect(() => {
  document.title = `You clicked ${count} times`;
}, [count]); // Only re-run the effect if count changes

With the following explanation:

In the example above, we pass [count] as the second argument. What does this mean? If the count is 5, and then our component re-renders with count still equal to 5, React will compare [5] from the previous render and [5] from the next render. Because all items in the array are the same (5 === 5), React would skip the effect. That’s our optimization.

However, as the array we're shown only has a single element it, it is ambiguous what "all the items in the array" means. E.g. we cannot tell whether that's intentional phrasing, or just "plain English" that isn't actually tied to any technical precision.

Could this phrasing either be made more explicit, or (ideally) can an extra paragraph be added to explain what happens to an array with more than one element?

(originally filed as from https://github.com/facebook/react/issues/15068)

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 issue 中链接的 docs/hooks-effect.html 的“Tip: Optimizing Performance by Skipping Effects”部分开始。检查对 [count] 依赖数组的说明,并明确当数组包含多个元素时比较的工作方式。当文档明确且无歧义地解释多元素情况时,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, react
领域
documentation
Issue 类型
文档
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
描述清楚
新手友好度
55/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。