improve documentation for useEffect hook
Personne n'a encore pris cette issue.
- Langage dominant
- JavaScript
- Étoiles
- 11.8k
- Forks
- 7.9k
- Merge moyen
- 1 j 11 h
- PR mergées (30 j)
- 11
Description
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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par lire la section liée « Conditionally firing an effect » et l’issue #15154 pour consulter l’explication existante de la comparaison des dépendances. Précisez que React compare les valeurs des dépendances entre les rendus avec Object.is et ne surveille pas les mutations à l’intérieur des objets référencés ; le travail est terminé lorsque la documentation de useEffect lève toute ambiguïté autour des « changements ».
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- javascript, react
- Domaine
- documentation
- Type d'issue
- Documentation
- Difficulté
- 2/5
- Temps estimé
- 1-3 heures
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 42/100