Use a better example for Effect hook
还没有人认领这个 Issue。
- 主要语言
- JavaScript
- 星标
- 11.8k
- 派生
- 7.9k
- 平均合并
- 1 天 11 小时
- 30 天内合并 PR
- 11
描述
The following example is used in:
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
// Similar to componentDidMount and componentDidUpdate:
useEffect(() => {
// Update the document title using the browser API
document.title = `You clicked ${count} times`;
});
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
This example creates confusion because the example doesn't actually need useEffect to operate correctly.
If you set document.title outside of the useEffect, the app behaves exactly the same.
Suggested changes
-
Use an example which would not work correctly if it wasn't placed in a
useEffect. It should be something that depends on the DOM having been updated. Maybe setting thedocument.titleto adocument.getElementByIdor something like that. This would also be a contrived/useless example. Something practical would be better. -
Show the lifecycle of useEffect early. I was most interested in the lifecycle of when this method is called. In Dan's recent blog post on effects, this is the first thing that is covered, and it should be the same for these docs.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
先从 hooks-overview.html#effect-hook 和 hooks-effect.html 中的 Effect Hook 示例开始,然后将当前示例与所引用文档中的生命周期讨论进行比较。用一个能够说明为什么需要 useEffect 的示例替换令人困惑的示例,并更早介绍其生命周期;完成的标准是两个页面都能一致地解释其行为,并展示改进后的示例。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, react
- 领域
- documentation
- Issue 类型
- 文档
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100