Update Documentation and/or ESLint rules to include an example for conditional parameters passed to `useState()`
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 11.8k
- Forks
- 7.9k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 11
Description
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)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked StackOverflow post and the React ESLint Plugin behavior around useState after a for loop, then compare it with the Hooks documentation. Done means the correct approach is decided and reflected in the relevant documentation and, if needed, the ESLint rule behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- eslint, javascript, react
- Domain
- documentation, tooling
- Issue type
- Documentation
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100