reactjs / reactjs/react.dev

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

Offen
#2,251 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
JavaScript
Sterne
11.8k
Forks
7.9k
Ø Merge
1 T. 11 Std.
Gemergte PRs (30 T.)
11

Beschreibung

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)

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem verlinkten StackOverflow-Beitrag und dem Verhalten des React ESLint Plugin in Bezug auf useState nach einer for-Schleife und vergleiche dies anschließend mit der Hooks-Dokumentation. Erledigt ist die Aufgabe, wenn der richtige Ansatz entschieden und in der relevanten Dokumentation sowie, falls erforderlich, im Verhalten der ESLint-Regel umgesetzt ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
eslint, javascript, react
Bereich
documentation, tooling
Issue-Typ
Dokumentation
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.