reactjs / reactjs/react.dev

Use case question regarding "You Probably Don't Need Derived State" article

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

还没有人认领这个 Issue。

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

描述

Hello,

I just finished reading: https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html and at the bottom of the article it states:

It is also worth re-iterating that getDerivedStateFromProps (and derived state in general) is an advanced feature and should be used sparingly because of this complexity. If your use case falls outside of these patterns, please share it with us on GitHub or Twitter!

My question regards complex components like Datatables which take in many arguments and which allow developers to externally change things about the table. For example, I may setup a table like this:

var options = {
    sortOrder: {field: "name", direction: "desc"},
}

<Datatable options={options} data={data} />

Internally the table allows users to change the sortOrder by clicking column headings. It manages to do this because it has it's own internal state which it derived from the input props. But lets say I want to change the sortOrder outside of the table.

One way to do this would be to change the options object and have the table re-render, it would then derive the sortOrder value from the props if present and use this for it's internal state. For table properties that aren't on props, the table would keep using it's internal state.

My question is - is this a good way of doing this, or is there a better way where state isn't derived from props? Comonents like a Datatable are very large and many of the properties should be uncontrolled, but there are certain properties that developers will sometimes want to control. Is this a good use case for using derived state?

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先,重新阅读链接的“You Probably Don't Need Derived State”文章以及本 issue 中的 datatable 示例。确定文章是否涵盖了所描述的、由外部控制并由内部管理的 sortOrder 混合情况;完成的标准是形成一份有文档记录的回答,或进行范围明确的文档更改。

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

评估

技术栈
javascript, react
领域
documentation
Issue 类型
文档
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
20/100

把新 issue 发到你的邮箱

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