Request for clarification: can ref initialization code (lazy or not) contain side effects?
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
New docs on avoiding recreating ref contents has two examples:
const playerRef = useRef(new VideoPlayer());
const playerRef = useRef(null);
if (playerRef.current === null) {
playerRef.current = new VideoPlayer();
}
Even the old docs have similar examples:
const ref = useRef(new IntersectionObserver(onIntersect));
const ref = useRef(null);
if (ref.current === null) {
ref.current = new IntersectionObserver(onIntersect);
}
Both new VideoPlayer() and new IntersectionObserver(onIntersect) look like calls that might contain side effects inside.
My question is it allowed in any of the above examples, for these calls to contain side effects?
-
imho in Examples 1/3 they should not because they are executed on each render; although only values from initial render are kept.
-
But examples 2/4 suggest a pattern which make reading and writing to refs ok - which normally is an impure operation already. So I thought maybe in those examples at least, it is ok for
new VideoPlayer()andnew IntersectionObserver(onIntersect)to contain side effects?
It would be helpful if the docs clarified this.
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 actual sobre useRef, “avoiding recreating the ref contents”, y la sección enlazada de la Legacy Hooks FAQ sobre la creación diferida de objetos costosos. Compara los cuatro ejemplos y verifica las indicaciones de React sobre los efectos secundarios durante el renderizado. Se considera terminado cuando la documentación explica claramente si estos patrones de inicialización pueden contener efectos secundarios y distingue su comportamiento.
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
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 42/100