reactjs / reactjs/react.dev

Conditionally controlled component with `value` and `defaultValue` properties

Open
#3,407 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

In our project codebase there is an uncontrolled component that allows you to specify an initial value with value property (code examples are simplified just to show an idea):


const CustomInput = ({ value = '', onChange }) => {
  const [uncontrolledValue, setUncontrolledValue] = useState(value);

  const handleChange = (e) => {
    setUncontrolledValue(e.target.value);
    if (onChange) {
      onChange(e.target.value);
    }
  }

  return <input value={uncontrolledValue} onChange={handleChange} />
}

This component is used inside the complex form (other input components in this form are controlled). Resetting the form causes problems with this particular component because it doesn't update the internal value on props change. There is the article that recommends 2 possible solutions:

  • rewrite component to be controlled one
  • trigger force rerender of the component using key property

IMO there is another option available - make component controlled/uncontrolled depending on existence value property:


const CustomInput = ({ value: controlledValue, defaultValue = '', onChange }) => {
  const [uncontrolledValue, setUncontrolledValue] = useState(defaultValue);
  const isControlled = value !== undefined;
  const value = isControlled ? controlledValue : uncontrolledValue;

  const handleChange = (e) => {
    if (!isControlled) {
      setUncontrolledValue(e.target.value);
    }

    if (onChange) {
      onChange(e.target.value);
    }
  }

  return <input value={value} onChange={handleChange} />
}

This approach allows custom components to act as a standard <input /> element. I saw it in @kentcdodds video lesson and also in some popular libraries sources. But I can't find anything about such an approach in official react documentation. defaultValue is mentioned only here:

Should this approach be added as a recommendation in You Probably Don't Need Derived State or Forms article? If not are there any pitfalls to consider it as bad practice / anti-pattern?

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 “You Probably Don’t Need Derived State,” Forms, and Uncontrolled Components documentation linked in the issue. Compare their existing guidance with the controlled/uncontrolled component pattern described here, then determine whether a documentation change is warranted and where it should live.

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
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.