improve documentation for useEffect hook
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
useEffect takes an array of values that may cause the effect to rerun if they "change".
Two comments: First, what is "change?" From this issue I learned that React uses Object.is to that end, but the documentation for conditionally firing an effect does not state this.
Second, here's what the documentation says:
To implement this, pass a second argument to useEffect that is the array of values that the effect depends on. Our updated example now looks like this:
useEffect(
() => {
const subscription = props.source.subscribe();
return () => {
subscription.unsubscribe();
};
},
[props.source],
);
Now the subscription will only be recreated when props.source changes.
The wording "when props.source changes is confusing and ambiguous (in my opinion).
For instance, if props.source refers to an object with property x, and some other code, or even code in the same scope, does props.source.x++, then did props.source change?
I think - correct me if I'm wrong - or better, update the documentation - I think that what is meant if the value passed at one call to useEffect() is different from the value passed at another call that happens during a subsequent call to the function in which the call to useEffect is embedded, then the value in this position of the array is considered to have changed. Two values v1 and v2 are considered different iff Object.is(v1, v2) returns false. React does not try to somehow "watch" any objects that are being passed to useEffect's dependency array.
I think this follows from thinking about how useEffect could possibly be implemented, but it would help the reader to state it clearly.
It would also help if the notion of "change" is more clearly defined as "change across calls to the function in which the call to useEffect is embedded, " in my opinion.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem Lesen des verlinkten Abschnitts „Conditionally firing an effect“ und von Issue #15154, um die bestehende Erklärung des Abgleichs von Abhängigkeiten zu verstehen. Stelle klar, dass React Abhängigkeitswerte über Renders hinweg mit Object.is vergleicht und keine Mutationen innerhalb referenzierter Objekte überwacht; abgeschlossen ist die Aufgabe, wenn die useEffect-Dokumentation die Mehrdeutigkeit rund um „Änderungen“ beseitigt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, react
- Bereich
- documentation
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100