improve documentation for useEffect hook
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- JavaScript
- Estrellas
- 11.8k
- Forks
- 7.9k
- Merge medio
- 1 d 11 h
- PR fusionados (30 d)
- 11
Descripción
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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Empieza leyendo la sección enlazada “Conditionally firing an effect” y el issue #15154 para consultar la explicación existente sobre la comparación de dependencias. Aclara que React compara los valores de las dependencias entre renders con Object.is y no observa las mutaciones dentro de los objetos referenciados; se considera terminado cuando la documentación de useEffect elimina la ambigüedad en torno a los “cambios”.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript, react
- Área
- documentation
- Tipo de issue
- Documentación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 42/100