setState() callback function: potential confusion of call order
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
This paragraph from the React.Component reference page:
setState()does not always immediately update the component. It may batch or defer the update until later. This makes readingthis.stateright after callingsetState()a potential pitfall. Instead, usecomponentDidUpdateor asetStatecallback(setState(updater, callback)), either of which are guaranteed to fire after the update has been applied.
can create the impression that instead of doing:
this.setState({ value: 5 });
console.log(this.state.value); // => not necessarily 5
you can use the callback function to safely read the state back:
this.setState({ value: 5 }, () => console.log(this.state.value)); // => "guaranteed" 5
(especially if you draw a parallel between the callback function and the updater function)
and it may actually work in some cases where there's no setState batching, but as more calls get batched in the future, these constructs may generate unintended side-effects.
Instead, we could clarify that all callback functions for a batch of setState() calls get called after the entire batch has been processed.
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
Comienza con la sección setState de la página de referencia de React.Component enlazada en el issue. Revisa la redacción sobre los callbacks y el batching, y actualízala para que no haya ambigüedad sobre cuándo se ejecuta el callback y qué state está disponible después de un batch; se considera terminado cuando los ejemplos no pueden sugerir que un callback observa únicamente su actualización individual.
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
- Bien especificado
- Aptitud para principiantes
- 45/100