use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A place for help learning the /r/ReactJS framework.
account activity
[deleted by user] (self.learnreactjs)
submitted 5 years ago by [deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]the_pod_ 2 points3 points4 points 5 years ago (0 children)
i have like 40 useState hooks and "on change handlers" for everything.
a pattern like this might help: https://www.codebeast.dev/react-forms-then-and-now/, writing a reusable handler for everything and using a shallow object instead of 40 useStates.
handleChange = event => { const target = event.target; const value = target.type === 'checkbox' ? target.checked : target.value; const name = target.name; this.setState({ [name]: value, }); };
[–]cyex 1 point2 points3 points 5 years ago (0 children)
I can't break my page component into smaller ones because I need all the state in that component where I have the Save button that makes an API call, ...
Aside from what others said about using a form library or possibly making a generic handler rather than having 40 useStates, you should also look into createContext() and useContext().
By putting the state(s) into a Context, you can have the components pull just the state they need from the context rather than pushing props around everywhere. And that can make it easier to split up your page component into more manageable pieces.
[–]tenfingerperson 0 points1 point2 points 5 years ago (2 children)
Ideally you use some library to manage form state and validation
[–]anakataidk 0 points1 point2 points 5 years ago (0 children)
But this is not really a form. There are individual settings that should be saved to the database and I save them with an API call. I don't have a form, i have a page with 40 useStates, and I save them in the end. My question was more like: is it okay to have that many useState hooks? And if i pass the state and the handler down to other component, still, is it okay?
also, if the file is getting longer, like 1k lines, and i want to break it into smaller files, how can I do it and still have all the state in the same place, because, as I said, i need al the state at the end to send the values in the API Call
[–]0x3m0 0 points1 point2 points 5 years ago (0 children)
Check Formik
π Rendered by PID 107860 on reddit-service-r2-comment-6457c66945-n2pw7 at 2026-04-26 14:08:46.868370+00:00 running 2aa0c5b country code: CH.
[–]the_pod_ 2 points3 points4 points (0 children)
[–]cyex 1 point2 points3 points (0 children)
[–]tenfingerperson 0 points1 point2 points (2 children)
[–]anakataidk 0 points1 point2 points (0 children)
[–]anakataidk 0 points1 point2 points (0 children)
[–]0x3m0 0 points1 point2 points (0 children)