reactjs / reactjs/react.dev

Aren't updater functions in state an antipattern?

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

还没有人认领这个 Issue。

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

描述

In documentation here:
https://reactjs.org/docs/context.html#updating-context-from-a-nested-component

An updater function is placed directly in the state object for the sole purpose of passing it down through context:

this.state = {
  theme: themes.light,
  toggleTheme: this.toggleTheme,
};

This seems like an odd antipattern. I've never placed a method in state before. Shouldn't state be exclusively for properties that affect rendering and change over time?

贡献指南

打开贡献指南

从这里开始

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

调研方向

审阅链接的 React Context 文档,尤其是“Updating Context from a Nested Component”部分及其中的 updater-in-state 示例。确定该示例是否需要澄清或修改,并在文档对这一模式给出清晰且达成共识的解释后,视为工作完成。

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

评估

技术栈
javascript, react
领域
documentation
Issue 类型
文档
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

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