Use case question regarding "You Probably Don't Need Derived State" article
還沒有人認領這個 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?
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
首先,重新閱讀連結的「You Probably Don't Need Derived State」文章,以及此 issue 中的 datatable 範例。確認文章是否涵蓋所描述的、由外部控制並由內部管理的 sortOrder 混合情況;完成的標準是提供一份有文件記錄的回答,或進行範圍明確的文件變更。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- javascript, react
- 領域
- documentation
- Issue 類型
- 文件
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 需要釐清
- 新手友好度
- 20/100