reactjs / reactjs/react.dev

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

Offen
#3,407 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
JavaScript
Sterne
11.8k
Forks
7.9k
Ø Merge
1 T. 11 Std.
Gemergte PRs (30 T.)
11

Beschreibung

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?

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der in der issue verlinkten Dokumentation „You Probably Don’t Need Derived State“, „Forms“ und „Uncontrolled Components“. Vergleiche deren bestehende Hinweise mit dem hier beschriebenen controlled/uncontrolled component pattern und bestimme anschließend, ob eine Änderung an der Dokumentation gerechtfertigt ist und wo sie ihren Platz finden sollte.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, react
Bereich
documentation
Issue-Typ
Dokumentation
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.