reactjs / reactjs/react.dev

Derived State: Fully uncontrolled component with a key

Open
#1,056 0 comments 6 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

Hi,

In the article You Probably Don't Need Derived State, one of the recommended ways to avoid deriving state from props, is to have "Fully uncontrolled component with a key".

However, there is no way to actually force the user to provide a 'key' property to the component, since declaring it in propTypes as "key: PropTypes.any.isRequired" won't work, because it's value is always 'undefined', which leads to a warning message:

Warning: MyComponent: key is not a prop. Trying to access it will result in undefined being returned. If you need to access the same value within the child component, you should pass it as a different prop.

And without declaring the 'key' prop type, we cannot assume, that the user will use the component in a proper manner.

How to mitigate this?

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 with the linked “You Probably Don't Need Derived State” article and the reported PropTypes warning about accessing key. Determine whether the documentation needs clarification about fully uncontrolled components with a key; done means the article explains a valid mitigation or explicitly documents the limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.