Introduction
When I was creating a signup form, I found myself creating dozens of useStates & then creating dozens of onChange handl...
For further actions, you may consider blocking this person and/or reporting abuse
That's it!
It was really hard on my last projects to make junior devs to understand that, because if you only learnt react that's what you'll find in the doc about managed/controlled forms so React is the only "source of truth".
That's useful on multi-step forms but I still can't find a way in which this is better than using the DOM itself through HTML.
Moreover you can set validations as well in the HTML using the pattern attribute and use
:valid
and:invalid
CSS selectors to style them depending on the state they're in (I thought I made a post on that stuff but I just realized I didn't so maybe I should cover all that stuff in a single place π).Yeah, that appears quite a large bite for a junior dev to gulp π
Thanks for the comment btw β¨
Yes but just because some years ago people learnt HTML to the detail, then CSS, then JS browser API and then frameworks/libs.
I'm finding lately a good amount of juniors that don't know what is the real scope of React... I've even told by one that
map
is a React method π so my guess is that the issue is the learning path itself...This is a legit issue, and new frameworks like svelte for example, make you learn more of "svelte" and not more of "javascript". Svelte is cool & I love it, but I think one should know fundamentals of Vanilla JS before dipping toes in libraries/frameworks
Totally agree!
Thanks for bringing this up! Just because youβre using JavaScript to render the elements doesnβt mean you shouldnβt also render markup that does its own work tooβ¦ use the tools for what theyβre made for!
Please do write a post and share all the knowledge you have.
I do it from time to time, check my profile!
Also follow to receive new posts in your feed π
Hi @anubha143 I finally bring this up, check it out! π
Form input validation WITHOUT JavaScript
JoelBonetR γ» Aug 15 γ» 2 min read
Nice way of handling forms. We have to highlight the limitations as well though. You still need
value
andonChange
when you want an input to update when another one is changed. Also if the form rerenders because of another component, your inputs will be cleared (happened to me with a component that shows the current time.) But for simple forms, where it's guaranteed that there will be no rerenders while you fill the form, it can save time.Form data is very nice yes :D For the cases where you need to do validation while the user is typing, or otherwise need to have controlled inputs, you can use something like Formik, or put state in a reducer:
@brense Input validation on the spot without even using JS at all:
Form input validation WITHOUT JavaScript
JoelBonetR π₯ γ» Aug 15 '22 γ» 2 min read
<input type="email" />
as example)Hmm, might give that a try... Have used the pattern prop before to force number keypad on phone etc, but never really considered it for actual validation... I do still think there's a use-case for linking actual state to the inputs when you want to do some calculated state for example, but for validation this looks pretty nice at first glance.
Thanks for sharing, cool article. Especially the
new FormData(e.target)
part make things more easy.My opinion: don't use "controlled components". If you have them, remove them and use the approach
<form onSubmit={..}>
. If you need validation, you can check input inonChange
and use likesetUsernameError(..)
if you need to really render the component to show an error, but not every keyboard hit will make it render.At least there are some libs for forms.
Thanks for the comment, Philipp :)
Yes, you're right, and I personally use the same approach too, but I have seen in codebases, sometimes people like to use this
value{ x } onChange={e => setX(e.target.value)}
Thought might include it as well!
Very cool, a much better way to interact with forms and save on that precious memory.
If you're going to be using this method a few times it's worth having a little hook with common functions built in, rather than writing them over and over.
Thanks for the code,
Great idea to abstract the logic into a hook ππ»
What about handling forms like this?
I've been using this method for some time now, without any issues
Very cool article! I did the same mistake in my early stages of react, generating a state for every property which I needed in my form :). Can you enlighten me one more. I used react-hook-form in the past. For controlled inputs I think it's unbeatable but would you still use the native html approach?
Man do what suits you best, Personally for me, I like to minimise package dependencies, thus would use the native HTML approach until it takes a lot of work to roll with it, then I would look for other alternatives as needed :)
Sounds legit. Not depending on a lib is always good. Can you do also dependant validation? Lets say if a=dog, b can only be "good Boy | bad Boy" ? Otherwise it has no validation.. smth like that? Is that possible with native HTML validators? I'm not experienced with that sorry if that sounds kinda stupid
Dont be sorry for asking stupid questions, NEVER.
Coming to HTML-native way of validating, HTML can check for empty fields but complex validation? no, you have to implement that yourself in the
onSubmit
handler.See this code : codesandbox.io/s/trusting-torvalds...
Try submitting the form without entering any value ;)
Thanks. Yeah I was aware of "simple" validation. Ok if I need something more complex I will continue using react-hook-form :)
This is quick and neat for very small or pet projects, but please use formik or react-hook-form for any real client production apps
Can you justify somehow this statement? There are better options
Not sure you've used this, but you can control them all in a single state with something like this:
Is it safe to say that generating multiple useStates for a form can cause a performance issue? Also I wonder why if there are better ways to handle input why donβt they teach that in bootcamps? Thatβs frustrating
This is what can be solved in Vue in one line
<input type=βemailβ v-model=βformData.emailβ />
?That and a lot more :D
I went from Vue to React and I can say it's a very very disappointing experience.
The only "plus" of React IMO is being popular.
Yeah, I've gone through the controlled form element exercise a few times but have decided using default html5 form validation and element types with some mods where needed does what I need without all of the event handling and state management overhead .
I'm personally annoyed by validations that pop up on element blurs, etc., and am perfectly happy with validation performed on submit. Standard pattern validation can provide most, if not all, of the oddball input requirement conditions, and overriding validation messages where needed to be more informative when needed.
Besides, IMO, this behavior is more expected. "Realtime" validation is unnecessarily noisy and can be clunky. Oh, and adds support overhead.
Using react native - I ended up going down this route myself naturally but I actually found that having the complexities that this brings also hindered me quite a bit from actually creating quickly.
The reusability is helpful but I feel like it makes code harder to read somewhat
I use react hook forms, data validation + performance + state handling all in one, and bundle size is optimum as well.
A good intro to the inner workings of forms. But yeah, when it comes to forms, donβt do it yourself is my recommendation! Formik is one of the best libraries Iβve worked with and would highly recommend for anything form related.
I understand this is just an HTML approach, but I encourage you to use libraries such as React Hook Form and Formik. Benefits are incomparable
Agreed π
Wait wait wait... Huh?
What is this sorcery? π
This sorcery is also called HTML β¨
Ah yes, the most darkest of dark arts - HTML π
react-hook-form is close to this vanilla approach and takes care of all edge cases, it us the best form lib out there for react.
Try something more better and extensible like Formik or Hook form
Cool article, is there a way to do custom field validations on typing using that form DATA ?
Nice article. I highly suggest taking a look at Formik and similar libraries, it seems like that's what the cool kids are using nowadays.
I kinda wanted to take an HTML native approach here :)
Ofcourse, if you've got a complex use-case, you'd go for formik or any other form handling library,
Thanks for the comment btw ππ»
Noice ππ₯
Thank you for this! I never thought that I can abuse html form tag for submitting forms.
I like this approach! Thanks for sharing. I graduated from a bunch of useStates, to using [e.target.name] approach, but this is far superior π― Can't wait to dive into it.
very good solution
Marked! Very helpful!
Glad you found it helpful π
That's awesome βΊοΈ
I actually used this on my last project. Thanks for throwing more light.
Thanks for sharing this Kuvam, saving us from writing so many useState code
Yeah! That's the proper way to handle forms without third-party library
The form fields lack labels in order to be compliant to WCAG 2.1 : w3.org/TR/WCAG21/#name-role-value
What about if uploading only image ?
Works just fine, mate ππ»
code: codesandbox.io/s/condescending-sha...
Nice articleβ¦ Iβd recommend Formik.
Thanks :)
Yeah formik is good but I wanted to discuss the HTML-way of handling forms ππ»
Reading the article and the comments is hilarious, and kind of sad. Together, they speak volumes about the awful way web development seems to be being learnt/taught these days.
Or better use react-hook-form + yup for easiest management of validators and error messages plus values.