STOP using useState for React forms (there's an alternative method)

  Рет қаралды 9,814

WebDevEducation

WebDevEducation

Күн бұрын

There's no need to use the useState hook when writing React.js forms.
⭐️ webdeveducatio...
🔔 Subscribe if you want more free tutorials like this one! kzbin.info...
#reactjs #nextjs #usestate

Пікірлер: 46
@WebDevEducation
@WebDevEducation Жыл бұрын
Also, use zod for your form validations 😉
@maker_aleks
@maker_aleks 11 ай бұрын
This approach is good, but… the best UX practice is to validate the field after it was unfocused for the forms longer then 3 fields.
@eduardoalvarez4457
@eduardoalvarez4457 4 ай бұрын
in theory you can use onChange() on the input fields and call a function that uses FormData to check the status. FormData is about removing the need of useState for each form. Rather to force people to only validate after submission.
@Thanveershah
@Thanveershah Жыл бұрын
Great video as always, A quick question, 1. How do we validate on change using this approach? 2. How does this work in case of having step form?
@WebDevEducation
@WebDevEducation Жыл бұрын
If you can, it would be easier to validate on form submit rather than validating every field on change. And for step forms can do the same, when next button is clicked to go to next step, validate the inputs from the current step. You could use useRef to grab the form element in this case.
@yashsolanki069
@yashsolanki069 11 ай бұрын
​@@WebDevEducationbut sometimes there is a requirement to validate fields as the input changes so i guess in that scenario we need to use useState. I'm wondering if having third-party lib like react hook form has any downside in terms of bundle size if not then third party lib is better to use right?
@WebDevEducation
@WebDevEducation 11 ай бұрын
@@yashsolanki069 yes for sure, if client side validation is required on certain fields then useState is recommended, or if the form is quite large, useReducer. Personally I prefer to use the least amount of 3rd party libs as possible and code things myself, unless (in the context of forms) the form is huge and requires a bunch of validation, then like many people have already suggested, a 3rd party lib really is the way to go. I always wait until I feel like a 3rd party lib is necessary tho, I never just default to using a 3rd party lib. As far as bundle size goes, you want to make sure that the lib supports tree shaking, which will keep the bundle size down. Another thing to consider is the complexity of setting up a form with a 3rd party lib. If it looks intuitive then it shouldn't be a problem, but if you're using a 3rd party lib for a small form that doesn't look so intuitive to set up, better to just keep the code simple and do it yourself imo. Also to make it clear these are just my opinions and despite what I said in the video, there is no right or wrong way 🙏
@behnoudmst
@behnoudmst 9 ай бұрын
Becuase I dont wana use zod or react hook form for small forms that have maybe 3 inputs, I was looking for this. Thanks a lot.
@saeedd2986
@saeedd2986 7 ай бұрын
you got rid of onchange to replace it whit something like useref you see there is a point that pepeol use onchange and onblur and onfocus to make the form be alot more user friendly(ux) but its good if you just need to show validation errors to users after they submit which not the best experience for users
@kashishrawat-b7f
@kashishrawat-b7f 11 ай бұрын
I use React useForm library , Its much better to use in Case of forms. Dosen't re-render the webPage on every keypress.
@WebDevEducation
@WebDevEducation 11 ай бұрын
👌
@maskman4821
@maskman4821 11 ай бұрын
Great tutorial, thank you 🙏
@BaraBrian
@BaraBrian 11 ай бұрын
Just use one of the many great form libraries out there. This will end up being more of a hassle than it's worth if you want to deliver a decent user experience
@WebDevEducation
@WebDevEducation 11 ай бұрын
Which is your go-to form lib?
@BaraBrian
@BaraBrian 11 ай бұрын
@@WebDevEducation My go-to is usually react-hook-form, mainly because of its light weight and performance. It also pairs up great with zod.
@meka4996
@meka4996 11 ай бұрын
Very simple form! Thanks
@Nethezbet
@Nethezbet 11 ай бұрын
Interesting, I did this with state but not near as...verbose? You do need the onChange for each, but only one (not a separate one for each entry) and then dump it into the state as an object. I appreciate that there are so many ways of doing the same thing, I just believe your state example was a bloated method that could be shrunk down significantly. Thanks for the video! I am still learning so it is SO helpful to see so many different ways of doing things.
@vbtux1
@vbtux1 Жыл бұрын
Great post,Thank you for sharing.
@fbrse
@fbrse 11 ай бұрын
Good! But what about two-way bind, in a item edit form?
@WebDevEducation
@WebDevEducation 11 ай бұрын
Then you'll probably wanna use useState :)
@7doors847
@7doors847 9 ай бұрын
Interesting approach 👍
@waytospergtherebro
@waytospergtherebro Жыл бұрын
Hooks are much better and easier to understand than class methods and the fact that there's a new video explaining how to use hooks "properly" every 12 seconds proves it conclusively.
@WebDevEducation
@WebDevEducation Жыл бұрын
It's click baity. Without useState is cleaner in my opinion, but people can do whatever they want 😆
@MaixPeriyon
@MaixPeriyon 11 ай бұрын
This isn’t good if you need to perform validation on a par element basis
@WebDevEducation
@WebDevEducation 11 ай бұрын
In that case you'd definitely want to use useState or a form lib to handle that :)
@marole-po9ne
@marole-po9ne 3 ай бұрын
That is for very simple forms only. It is the IMPROPER way for complex forms where there is validation , error handling and need to update the UI. useState and other react use* are the proper way for complex forms. Thanks anyway for this video
@omarrayes2281
@omarrayes2281 11 ай бұрын
does it cause rerender as useState when change the value ?!🤔
@Lukas----
@Lukas---- 4 ай бұрын
Any downsides to this method?
@WebDevEducation
@WebDevEducation 4 ай бұрын
Plenty. I'd only use this for simple forms that don't require any onChange handlers.
@PeriklesPeriklesoglu
@PeriklesPeriklesoglu 5 ай бұрын
thank you
@lavdev
@lavdev 11 ай бұрын
If you do that, how can you validate the form?
@WebDevEducation
@WebDevEducation 11 ай бұрын
If you want to validate every field use useState or a form lib - otherwise just have a single useState for error message
@ELMlKO
@ELMlKO 11 ай бұрын
amazing video
@deadwarrior9866
@deadwarrior9866 Жыл бұрын
it still not good if u face multiple field for different purpose
@WebDevEducation
@WebDevEducation Жыл бұрын
Sure, many instances where useState is preferred.
@mister.kosmos
@mister.kosmos 11 ай бұрын
next step. use library like formik for forms. and next step after that. use ui kit library like antdesign or material ui :D
@pfwu7251
@pfwu7251 Жыл бұрын
how to clear the form after submit
@WebDevEducation
@WebDevEducation Жыл бұрын
e.currentTarget.reset();
@arsalanwahidi9234
@arsalanwahidi9234 11 ай бұрын
let formInputs = event.target; let titleInput = formInputs.elements.your_Input_field_name; your_Input_field_name.value = ''; After successful submission reset value of the input to empty value.
@Chris-zt4ol
@Chris-zt4ol Жыл бұрын
Cool for simple forms but 1. not typesafe 2. no advanced validation 3. styling wont be fun
@WebDevEducation
@WebDevEducation Жыл бұрын
You can make it typesafe, and do any validation you want with zod. Why would this method mean styling would be different?
@Chris-zt4ol
@Chris-zt4ol Жыл бұрын
@@WebDevEducation FormData is not typesafe. When you use zod/javascript anyway, why not just use react hook form so you don't have to manage your forms lifecycle. Styling is no fun because you have to write your own logic to handle the form lifecycle + you can't show field specific error messages with FormData
@WebDevEducation
@WebDevEducation Жыл бұрын
Agreed and thats probably a separate discussion with react form. I use react form for more complex forms, but for the rest I waaaaay prefer formdata over a butt load of usestate / value / onchange. Styling tho I've never found it any more painful using formdata over usestate.
React JS crash course 2024 for beginners
39:40
WebDevEducation
Рет қаралды 7 М.
How To Get Married:   #short
00:22
Jin and Hattie
Рет қаралды 26 МЛН
Life hack 😂 Watermelon magic box! #shorts by Leisi Crazy
00:17
Leisi Crazy
Рет қаралды 45 МЛН
An Unknown Ending💪
00:49
ISSEI / いっせい
Рет қаралды 58 МЛН
My Daughter's Dumplings Are Filled With Coins #funny #cute #comedy
00:18
Funny daughter's daily life
Рет қаралды 12 МЛН
What You Missed in September
3:55
Awesome
Рет қаралды 21 М.
React Hook Form - Complete Tutorial (with Zod)
28:22
Cosden Solutions
Рет қаралды 118 М.
Goodbye, useEffect - David Khourshid
29:59
BeJS
Рет қаралды 500 М.
Why I avoid react's uncontrolled inputs
13:06
Web Dev Cody
Рет қаралды 14 М.
When to use server actions in Next JS 14
5:28
WebDevEducation
Рет қаралды 9 М.
10 Essential React Native Tips Every Developer Must Know
12:07
Simon Grimm
Рет қаралды 7 М.
Form Validation In React Using YUP Tutorial
14:22
PedroTech
Рет қаралды 93 М.
How To Get Married:   #short
00:22
Jin and Hattie
Рет қаралды 26 МЛН