reactjs / reactjs/react.dev

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

Open
#2,603 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
11.8k
Forks
7.9k
Avg merge
1d 11h
Merged PRs (30d)
11

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by rereading the linked “You Probably Don't Need Derived State” article and the datatable example in this issue. Determine whether the described mix of externally controlled and internally managed sortOrder is covered by the article; done would be a documented answer or a clearly scoped documentation change.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.