Skip to content
Advertisement

More elegant way of checking all state values

Is there a more elegant way of checking all state variables in my react app ? I currently have 14 state variables within my app, I am checking the value of each and updating to an empty string if they do not pass validation (left empty) code is as:

JavaScript

This does work, so its not the end of the world, but it just seems as though its not very elegant and I feel like there could be a more concise remedy out there?

Thanks

Advertisement

Answer

Maybe something along these lines. As all these state variables seem to be tightly coupled. I don’t see why they can’t be one object.

JavaScript

If you need to update one of them you can use spread.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement