reactjs / reactjs/react.dev

Update Documentation and/or ESLint rules to include an example for conditional parameters passed to `useState()`

Abierto
#2,251 0 comentarios 0 reacciones 0 asignados Ver en GitHub

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

I just wrote a StackOverflow post in which I found a workaround for the React ESLint Plugin seemingly erroneously giving an error that useState was being called conditionally when it was actually called after a for loop in top-level code.

As mentioned in the post, rather than moving the useState call before the loop, I needed to use that loop to set the inital value of the state. By using .forEach(), I was able to get rid of the error from ESLint and make the code work, but, as mentioned at the end of my answer

i'm pretty sure that tricking ESLint into not giving an error like this is not a good long-term solution. Ideally ESLint rules probably need updating to better check for improper use of conditionals, and/or the react docs should be updated with an example for how to conditionally set a hook's default value in a way that doesn't violate the rules of react hooks.

Because this solution feels like its a little hacky and not the best long-term solution to this problem, I wanted to get clarification on what would be the correct solution in this case and hopefully get the dosumentation and ESLint plugins updated to reflect this (if necessary)

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con la publicación enlazada de StackOverflow y el comportamiento de React ESLint Plugin en torno a useState después de un bucle for, y compáralo después con la documentación de Hooks. Se considera terminado cuando se haya decidido cuál es el enfoque correcto y este se haya reflejado en la documentación relevante y, si es necesario, en el comportamiento de la regla de ESLint.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
eslint, javascript, react
Área
documentation, tooling
Tipo de issue
Documentación
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
30/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.