| ▲ | kccqzy 3 hours ago | |
My biggest tip to reduce complexity of data validation if you are using React is to stop using React controlled components and switch to React uncontrolled components. They are an underused part of React. You usually don’t need React to handle every single keypress and every single character being entered by the user. In fact before React popularized it, it was unusual for form components to update on each key press; traditional desktop apps tend to validate when a field loses focus only, not on each key press. This has at least three benefits: (a) good for performance, (b) reduces unnecessary error UI when the user is in the middle of entering data, and (c) simplifies your own code by not having to deal with prefixes of valid input that’s not itself valid. | ||
| ▲ | yawaramin 2 hours ago | parent [-] | |
Also allows user scripts to interact with the forms, eg I can run a bookmarklet to fill out certain forms. With React controlled components all these changes are wiped out and reset with the state that React has in its app memory. | ||