Tech Lead/Team Lead. Senior WebDev. Intermediate Grade on Computer Systems- High Grade on Web Application Development- MBA (+Marketing+HHRR). Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
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 π).
Tech Lead/Team Lead. Senior WebDev. Intermediate Grade on Computer Systems- High Grade on Web Application Development- MBA (+Marketing+HHRR). Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
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
Tech Lead/Team Lead. Senior WebDev. Intermediate Grade on Computer Systems- High Grade on Web Application Development- MBA (+Marketing+HHRR). Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
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!
Tech Lead/Team Lead. Senior WebDev. Intermediate Grade on Computer Systems- High Grade on Web Application Development- MBA (+Marketing+HHRR). Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
Tech Lead/Team Lead. Senior WebDev. Intermediate Grade on Computer Systems- High Grade on Web Application Development- MBA (+Marketing+HHRR). Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
Nice way of handling forms. We have to highlight the limitations as well though. You still need value and onChange 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:
Tech Lead/Team Lead. Senior WebDev. Intermediate Grade on Computer Systems- High Grade on Web Application Development- MBA (+Marketing+HHRR). Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
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 in onChange and use like setUsernameError(..) 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.
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)}
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.
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.
Tech Lead/Team Lead. Senior WebDev. Intermediate Grade on Computer Systems- High Grade on Web Application Development- MBA (+Marketing+HHRR). Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
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
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.
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.
Hello Iβm a Jr. Full Stack Developer. Iβm a 5 year Army vet. My goal is to meet a lot of developers to collaborate and learn from in order to grow as a junior developer.
Location
Colorado, US
Education
Bachelors in Cyber Security and currently working on Masters in Software Engineering.
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
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 am passionate about creation, be it code or written. I believe that knowledge should be sharee. If we all gave a little bit of our time to helping the each other, the world would be a better place.
I am passionate about creation, be it code or written. I believe that knowledge should be sharee. If we all gave a little bit of our time to helping the each other, the world would be a better place.
I am a Frontend Engineer with a passion for building pixel-perfect web applications, with great user interface and user experience, and also teaching others what I learn I am interested in Web3, WebGL
Bulgarian Blogger. Technical SEO Guy. JS & Next.Js Developer. React.Js Ninja, Rapid Learner. Creator of BuhalBu's Kit - It's All About Fast & Sleek Next.Js Apps
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,
let Bio = { username: 'hyggedev', fullName: 'Chris Hansen', bio: 'I createβ‘οΈ fast modern, client-side websites with React and Javascript.', passion: ['Blog', 'Tweet', 'Code'] };
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.
I was born and raised in lagos, i specialize in building applications specific to the business needs of my clients. I have done several work in software development, front-end/back-end web, database m
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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 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 :)
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
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:
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
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.
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.
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
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.
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.
Wait wait wait... Huh?
What is this sorcery? π
This sorcery is also called HTML β¨
Ah yes, the most darkest of dark arts - HTML π
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 π
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
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 ππ₯
very good solution
Marked! Very helpful!
Glad you found it helpful π
The form fields lack labels in order to be compliant to WCAG 2.1 : w3.org/TR/WCAG21/#name-role-value
That's awesome βΊοΈ
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.
Try something more better and extensible like Formik or Hook form
Or better use react-hook-form + yup for easiest management of validators and error messages plus values.
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.