Update Documentation and/or ESLint rules to include an example for conditional parameters passed to `useState()`
Nessuno ha ancora preso questa issue.
- Lingua principale
- JavaScript
- Stelle
- 11.8k
- Fork
- 7.9k
- Merge medio
- 1g 11h
- PR unite (30g)
- 11
Descrizione
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)
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con il post collegato su StackOverflow e con il comportamento di React ESLint Plugin riguardo a useState dopo un ciclo for, quindi confrontalo con la documentazione di Hooks. Il lavoro è completato quando l'approccio corretto è stato deciso e risulta riflesso nella documentazione pertinente e, se necessario, nel comportamento della regola ESLint.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- eslint, javascript, react
- Ambito
- documentation, tooling
- Tipo di issue
- Documentazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 30/100