[Suggestion]: Documentation states mutation is "not recommended" when it fundamentally doesn't work, misleading beginners about how React state functions
还没有人认领这个 Issue。
- 主要语言
- JavaScript
- 星标
- 11.8k
- 派生
- 7.9k
- 平均合并
- 1 天 11 小时
- 30 天内合并 PR
- 11
描述
Summary
The React documentation states that mutating state is "not recommended" and that developers can "get away with it," when in fact mutating state doesn't trigger re-renders at all. This misleads beginners into thinking mutation is a working anti-pattern rather than fundamentally broken code that prevents React from updating components.
Page
https://react.dev/learn/updating-objects-in-state
Details
Multiple sections in the React documentation misleadingly describe state mutation as "not recommended" or something you can "get away with," when the fundamental issue is that mutating state doesn't trigger re renders. This appears in at least:
The tic tac toe tutorial's "Why immutability is important" section
The last "Deep Dive" in the "Updating Objects in State" page in the Adding Interactivity section
Both sections list various reasons why mutation is discouraged (debugging, optimizations, features, etc.) but bury or omit the primary issue: if you mutate state, React won't re render your component because it can't detect the change.
The "Updating Objects in State" page is particularly problematic as it says:
"In practice, you can often 'get away' with mutating state in React"
My experience as a beginner:
This kind of language is incredibly confusing when learning React. React detects state changes by checking if the reference to the state has changed. When you mutate an object or array, you're modifying the same reference, so React doesn't know anything changed and doesn't re-render. Yet the official documentation makes it sound like mutation is just a bad practice that technically functions. When I read "not recommended" and "you can get away with it," I thought it was like using var instead of const, discouraged but functional most of the time.
The documentation's phrasing suggests there are scenarios where mutation works, but React fundamentally requires a new reference to detect changes. Mutation will not trigger a re-render.
Why this matters:
• React.dev is the official resource for learning React
• These are fundamental concepts that beginners encounter early
• This framing suggests mutation is a working anti-pattern rather than broken code
• Beginners won't understand that immutable updates are required for React to function, not just a best practice
Suggested improvement:
All sections discussing state mutation should lead with something like this:
"Mutating state directly doesn't trigger re-renders. When you mutate an object or array in place, React cannot detect the change because the reference remains the same. Your component will not re-render update."
Then follow with the additional benefits of immutability. Remove phrases like "get away with" that suggest mutation sometimes works. It only appears to work when other factors trigger re-renders, which should be made more clear.
Scope:
This language should be audited across all documentation pages that discuss state updates to ensure consistent, accurate messaging about why immutability is required, not just recommended.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 tic-tac-toe 教程中的“Why immutability is important”部分以及“Updating Objects in State.”中的最后一个“Deep Dive”开始。审查其他状态更新文档中是否有类似措辞,然后修改这些解释,使重新渲染的后果先于次要收益得到明确说明;当受影响的页面都一致地解释直接修改为何会失败时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- react
- 领域
- documentation
- Issue 类型
- 文档
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 38/100