React Form Validation Using Custom Hooks Tutorial - Beginner React JS Project

Поделиться
HTML-код
  • Опубликовано: 31 май 2024
  • Learn how to build a simple form in React with validation using custom hooks in this beginner React JS Project tutorial. We will only build the frontend for this form and display a simple error message if the inputs are not correct. Feel free to refactor the code and add this design to a backend or firebase.
    If you want to follow me along my coding journey, be sure to subscribe :)
    / @briandesign
    Source code
    github.com/briancodex/react-f...
    Timeline:
    0:00 React form we're building
    1:13 Creating React App
    4:00 Creating Project Files
    6:55 Editing FormSignup Component
    13:51 Creating Custom useForm hook
    22:07 Creating Validation Code
    28:57 Displaying Error Messages
    32:48 Creating Submit Form Function
    35:24 Display Success after submitting
    38:58 Adding the CSS
    41:30 Submit only if zero errors
    React Todolist App Tutorial
    • React Todo List App Tu...
    Responsive React Website Tutorial
    • React Website Responsi...

Комментарии • 200

  • @douglasfrb
    @douglasfrb 3 года назад +12

    I wish I had seen this tutorial a few weeks ago for an interview. It's pretty tricky validation in React when you're a beginner learning it. Thanks for sharing it!

  • @username_0_0
    @username_0_0 3 года назад +7

    I loved the way you say *boom!* every time. That keeps the level of energy up.

  • @MarkSatin1
    @MarkSatin1 2 года назад

    Thanks so much for this video. I really appreciate how you just explain things very simply and directly and also how you explain *every* step you show.

  • @indojojon892
    @indojojon892 2 года назад

    hey thank you for the tutorial. It really helps me learning react hooks, especially custom hook

  • @nassimyazdani9398
    @nassimyazdani9398 3 года назад

    Thank you very much for your efforts. Your videos are perfect. You explain everything in detail. Super easy to follow. Keep up the good work.

  • @Cdswjp
    @Cdswjp 2 года назад

    I like how u show little extra tips, such as clicking SHIFT & SPACE to finish naming the component

  • @pccmedia7482
    @pccmedia7482 3 года назад

    Very Clean design pattern. Thank you

  • @MrKaremnour
    @MrKaremnour 3 года назад +13

    Actually this is the best validation i have ever seen
    ..to the point

    • @rohitsaxena7996
      @rohitsaxena7996 3 года назад

      I m facing problem regarding this method

  • @mongolmaagurbazar3058
    @mongolmaagurbazar3058 3 года назад +2

    Thank you very much for this wonderful tutorial. It's truly awesome!

  • @andersbraathen1256
    @andersbraathen1256 Год назад

    Great tutorial, thanks!

  • @sobdjs
    @sobdjs 3 года назад

    Another great video! You're an awesome tutor

  • @zahidshaikh3122
    @zahidshaikh3122 2 года назад

    Helped me out a lot, Thanks man...

  • @louischristopherchen
    @louischristopherchen 3 года назад +2

    WOW, you make it sound easy with your explanation bro. I come here after watching a responsive sidebar. And this completes my way to create my own library bro. Thank you so much, bro. I'm falling in love with how you doing your code.

  • @viliangaming5409
    @viliangaming5409 3 года назад

    Your tutorials are too awesome...I loved it so much....

  • @benisonsimao688
    @benisonsimao688 3 года назад

    You are the best Brian

  • @Blastsful
    @Blastsful 3 года назад +2

    Thank you Brian for another awesome tutorial !
    I have made some minor adjustment to the cross button appearing on the FormSignUp page. It will now only appear on FormSuccess page which is after submitting the form, and clicking it will refresh the page.
    import React from 'react'
    import './Form.css'
    const FormSuccess = () => {
    const refreshPage = () => {
    window.scrollTo(0,0);
    window.location.reload();
    }
    return (

    x

    We have received your request!



    )
    }
    export default FormSuccess

  • @huyanho2854
    @huyanho2854 3 года назад

    thank you for your instruction. It is really help me a lot.

  • @abdulbasitsalah2918
    @abdulbasitsalah2918 3 года назад

    just the video i need for my project

  • @sudarshanpradhan5388
    @sudarshanpradhan5388 3 года назад

    Hey Brian this tutorial is Awesome 👌 Please make more projects on reactJS

  • @andreitryfanau7625
    @andreitryfanau7625 3 года назад

    Amazing video and to the point! Would be amazing to see how a similar form can be implemented with an email sender. I would be curious to see how can you gather client's info and send it via email to yourself

  • @shawnf3495
    @shawnf3495 2 года назад +3

    Amazing project tutorial. Thank you. Just a few things the linter complained about. The first one was /anchor-is-valid. For the anchor tag used in the span tag right below the button in FormSingup.js the linter wanted a valid href attribute. I always thought '#' was valid but i guess not because it complained about it so i changed it to href="./". The second linter that complained was /img-redunant-alt which was referring to the alt attribute on the img tag in FormSuccess.js. It said "redunant alt attribute. Screen-readers already announce 'img' tags as an image. You don't need to use words 'image', 'photo' or 'picture' (or any specified custom words) in the alt prop." So I just changed it to alt="success-img". lol funny the linter didn't complain i changed it to that cause it's still redundant. And the last linter that complained was /exhaustive-deps which said "React Hook useEffect has missing dependencies: 'callback' and 'isSubmitting'. Either include them or remove the dependency array. If 'callback' changes too often, find the parent component that defines it and wrap that definition in useCallback." Dan Abramov addressed this issue and you can read more about it at the link i provided but basically he said generally speaking it is NOT safe to omit functions from the list of dependencies. I'm not experienced enough to know if listing all three functions in the dependencies will have any negative side effects. I changed it to include all functions in the list of dependencies for useEffect and it doesn't seem to have broken the error messages. But I would like to know was including all three functions [callback, errors, isSubmitting] in the list dependencies for useEffect the "correct" fix or is there another way address the issue that's more suitable?
    reactjs.org/docs/hooks-faq.html#is-it-safe-to-omit-functions-from-the-list-of-dependencies

  • @ruchiraghav1651
    @ruchiraghav1651 2 года назад

    amazing experience 🤩

  • @ruchiraghav1651
    @ruchiraghav1651 2 года назад

    thank so much for sharing useful content..

  • @zhoraamiryan5980
    @zhoraamiryan5980 3 года назад

    Coool thank you so much!

  • @shivapanahi7114
    @shivapanahi7114 3 года назад

    thank you so much.it was useful.

  • @YousufJusani
    @YousufJusani 3 года назад

    Sir, This is awesome 🔥

  • @brian-lau
    @brian-lau 3 года назад +2

    the code is sooo clean...
    that callback() in useEffect amazed me, i didnt know you can callback to the parent's function like that

    • @julianb.6580
      @julianb.6580 3 года назад

      Hey do you mind explaining that somehow? I went on mdn read about callback functions and watched some youtube videos about it but I still don't understand why callback is being used there?

    • @julianb.6580
      @julianb.6580 3 года назад

      Does the callback just run the custom hook again or what exactly is its job here? thanks in advance :)

    • @brian-lau
      @brian-lau 3 года назад

      @@julianb.6580 Yes hooks is meant for code re-use. And in this case, it firing a call-back to run the custom hook.

    • @julianb.6580
      @julianb.6580 3 года назад

      @@brian-lau thanks man appreciate the response!

    • @jadbennouna2182
      @jadbennouna2182 2 года назад

      @Brian Thank you for pointing this part of the tutorial. I was actually looking for an explanation about the callback() use. :D

  • @Gangbuster74
    @Gangbuster74 3 года назад +1

    This guy is awesome 🔥🔥🔥🔥please make a tutorial series on react

  • @BigToasti
    @BigToasti 3 года назад +1

    Thanks again ! :)

  • @luisespinosallanos3142
    @luisespinosallanos3142 2 года назад

    Yooo man, thanks

  • @melissagonzalezn
    @melissagonzalezn 2 года назад

    You're a hero

  • @babygracegarganera9667
    @babygracegarganera9667 2 года назад

    thank you so much!

  • @XDandrewsDX
    @XDandrewsDX 2 года назад

    Thank you !!

  • @vimalvestron8684
    @vimalvestron8684 2 года назад

    Good to move 'validate' inside useForm just for more abstraction :)

  • @sakshamkapoor5905
    @sakshamkapoor5905 3 года назад +1

    This is so lit ❤ 🔥

  • @yaeltzruia8157
    @yaeltzruia8157 2 года назад

    I had an error trying to make the error message work and I searched for the problem for houuuuuuurs and then I saw that the problem was that I didn't return the errors... people don't forget to retuuuuuuuurn 😂😅 Amazing video btw!!! Love the structure of the code and everything is really clear 🤗

  • @ErmaktheCreator
    @ErmaktheCreator 3 года назад

    Very good!!!

  • @charliebrewer1668
    @charliebrewer1668 2 года назад

    Thank you

  • @thestudypath
    @thestudypath 3 года назад +1

    Lit AF
    Subcribed

  • @christianmarucco5296
    @christianmarucco5296 2 года назад

    great video, but what plugin are u using to show those colored lines that mark matching brackets

  • @joshuabanks4110
    @joshuabanks4110 3 года назад

    Nice work Brian. Like to see how you've progressed over the last couple months. Very helpful videos. Thanks.
    Which VS Code Icon theme/extension are you using?

    • @briandesign
      @briandesign  3 года назад +2

      Thanks Joshua! I'm using vscode-icons and Community Material Theme Palenight High Contrast

    • @saivivekravi4979
      @saivivekravi4979 3 года назад

      @@briandesign but brain when I apply it it does not match the theme what is display on the video as colour saturation is different like that

  • @deficrypto1234
    @deficrypto1234 3 года назад

    Hi, Thanks for the tutorial. I get the following error in my index.js file ' Parsing error: Unexpected token, expected ",". Any suggestions please?

  • @nikolozabashia9987
    @nikolozabashia9987 2 года назад

    thank you

  • @klee.dodoco
    @klee.dodoco 3 года назад +4

    @Brian Design what is your callback() in useEffect doing? it seems like you've only called and defined it once. Thanks for the video though

    • @fedet22
      @fedet22 3 года назад +3

      The callback function is the submit form that you pass as a prop into the custum hook useForm. I always call it just like I difened it (submitForm)

  • @simplehumancomplexambition9884
    @simplehumancomplexambition9884 2 года назад +1

    Dude, I love your tutorials. I have a question about this one, why didn't you use styled-componenets? Just curious. By the way, get a Patreon started, I'd love to support your work a bit; you've helped a lot.

  • @bansodenavanath695
    @bansodenavanath695 3 года назад

    So nice tutorial!
    create by using Redux as well

  • @sourabpramanik996
    @sourabpramanik996 3 года назад

    Hey Brian can you tell me how we can use the useForm hook for radio buttons having different values?

  • @yasmimbarbosa4839
    @yasmimbarbosa4839 3 года назад

    So nice tutorial!
    What you use to send emails in contact form with react? For example: firebase, nodemailer or some like this?

  • @AJAYKUMAR-gf8jc
    @AJAYKUMAR-gf8jc 2 года назад

    Thanks

  • @rajtkbabu
    @rajtkbabu 2 года назад

    hi, thanks for the tutorial , how I can dibbled the button once submit , prevent for repeat click..

  • @earthycoin
    @earthycoin 3 года назад

    Brians a beast

  • @matteodirollo6157
    @matteodirollo6157 2 года назад

    Hey thank you for the tutorial it was super helpful but I'm having an issue with the callback function:
    Uncaught TypeError: callback is not a function
    Idk why but this seems to not work in my project. Does anyone know what might be wrong?

  • @RiyasMohammed-ib9jy
    @RiyasMohammed-ib9jy 3 года назад +1

    I am your indian fan, always smile and post new video.

  • @m..2694
    @m..2694 3 года назад

    First like and comment..
    But watch whole video....am sure it will awesome..

    • @briandesign
      @briandesign  3 года назад +1

      2nd! Thanks Mohit

    • @m..2694
      @m..2694 3 года назад

      @@briandesign 2nd..? Awesome video bro..am starting my react js practice and you have been really helpful,.have a fantastic day

    • @briandesign
      @briandesign  3 года назад

      @@m..2694 I was saying I was the 2nd comment lol and awesome to hear! have a great day as well!

  • @HANSACEROHERNANDEZ
    @HANSACEROHERNANDEZ 3 года назад

    hi good videos i am try to used your code for validation my fields but i use function in the component and ever out a error default is not a function or its return value is not iterable... do you have something that help me ?

  • @Saikumar-kb4lf
    @Saikumar-kb4lf 3 года назад

    How to make that right side scrollbar disappear for this login page alone, assuming we have to render scrollbar once we login

  • @cruquen
    @cruquen 2 года назад

    Hello Brian. great viode!! one question tho. i couldn't get to formSuccess after submitting. I tried the useEffect code shown below comments. the error is
    TypeError: Object(...) is not a function
    without useEffect. I can see useEffect is not used at all. does anyone know how to get around the error? thanks

  • @passsacaglia
    @passsacaglia 2 года назад +1

    Do you have a part 2 for the Login and validation+authentication in let's say a localhost or db that it checks the un+pw to the server and you log in to your "store" or your startpage ?
    Would be DOPE Brian!

  • @abishkarjoshi8514
    @abishkarjoshi8514 2 года назад

    How can I use the same validation on handleChange ? I want to make the same validation on each input change too.

  • @LucasRodrigues-ze3yd
    @LucasRodrigues-ze3yd 3 года назад

    Why does the first time I change some input, the value returns null? I mean, If I write an "a" inside the name input and console log values.name, it will return """, and if I write another "a", it will console log only "a"", and not "aa".

  • @roshanstepit
    @roshanstepit 3 года назад

    Please also guide as to how we can validate file and image uploads . using extenshion check and otherwise too. Thanks in advance

  • @r4k4210
    @r4k4210 2 года назад

    Is a good practice create customHooks for differents Components?, I see that is really clean way to separate code from component.

  • @Netkravler
    @Netkravler Год назад

    U need and arrow in your setValues , setValues(prev => ({...prev, [name] : value })

  • @lakshmiprabha12
    @lakshmiprabha12 3 года назад +5

    Hi Brian, thanks for this tutorial! i just ran into an error while i tried to deploy it in Netlify..
    React Hook useEffect has missing dependencies: 'callback' and 'isSubmitting'. Either include them or remove the dependency array. If 'callback' changes too often, find the parent component that defines it and wrap that definition in useCallback react-hooks/exhaustive-deps.
    Can you help me fix this issue?

    • @julianb.6580
      @julianb.6580 3 года назад +5

      Hey if you haven't already fixed this, you can fix it by going to your submit handler and wrapping it in a useCallback. I will give you an example using his code.
      So in his Form component he has a submit handler which looks like this:
      function submitForm() {
      setIsSubmitted(true);
      }
      What you want to do is import UseCallback from react and then change the submit handler to be like this:
      const submitForm = useCallback(() => {
      setIsSubmitted(true)
      }, [setIsSubmitted])
      Then also in the custom hook so the useForm component you would want to add the callback and the isSubmitting as a dependancy to the useEffect like this:
      useEffect(() => {
      if (Object.keys(errors).length === 0 && isSubmitting) {
      callback()
      }
      }, [errors, isSubmitting, callback])
      and now it should work :) basically the callback here is referring to the submit function, or thats just my understanding.
      Hope this helps you

    • @jellepaulus
      @jellepaulus 2 года назад +1

      @@julianb.6580 works for me, thanks!

    • @lakshmiprabha12
      @lakshmiprabha12 2 года назад +1

      @@julianb.6580 thanks a lot ! it worked

  • @abhijeetshikharvlog1444
    @abhijeetshikharvlog1444 2 года назад

    I have one doubt .Suppose i have multiple forms signup,signin,contactus,etc so where i define state in useForm component if i define here then the state is too large accroding to requirement

  • @ahmedshamoon12
    @ahmedshamoon12 3 года назад

    I love the Video but the form size does not changes when we minimize the browser...plz help.

  • @demetrekurdadze9530
    @demetrekurdadze9530 2 года назад

    hey, why does formSucsess and formSignup divs ahve same name? form-content-right?

  • @kokala2950
    @kokala2950 3 года назад

    Great video!! Can you please make a tutorial on how to do a sticky/fixed navbar while scrolling in react

    • @briandesign
      @briandesign  3 года назад +1

      have you seen the react navbar videos I've made? check out my react website vids and see if that is what you're trying to make

  • @klaudiawojcieska8735
    @klaudiawojcieska8735 2 года назад

    Hi! Great tutorial :) What extensions do you have? When I type .form-inputs nothing happens and u have some cool tool which autocompletes the code. Any tips?

  • @ChicClique
    @ChicClique 3 года назад

    Everything is fine, but I have a real situation here, that I want to dispatch the input values to the database. How & where can I get hold of them and dispatch? I thought I will dispatch the values from the FormSuccess page, but I didn't know how can I actually get the final values inside this page. Any suggestions Brian?

  • @abdulbasitsalah2918
    @abdulbasitsalah2918 3 года назад

    what is the name of theme that you are using in your vscode?

  • @thanhtung1482013
    @thanhtung1482013 2 года назад

    Excuseme , sir could i video access the video tutorial link for the tutorial React Form Validation Using Custom Hooks with Database Backend?

  • @ianlim5399
    @ianlim5399 3 года назад

    How do i implement this with axiom for backend?

  • @veenakatti2825
    @veenakatti2825 3 года назад

    Thank you for this tutorial. Its very useful. Subscribed

  • @kobpongkongrod5450
    @kobpongkongrod5450 Год назад

    Can anyone explain what is the purpose of the callback() inside the useEffect?

  • @oomkaarkhamitkar5499
    @oomkaarkhamitkar5499 2 года назад

    hey i use this in my code but they said TypeError: callback is not a function in useForm.js file can you give me some suggestion?

  • @shujathaliirfan2662
    @shujathaliirfan2662 2 года назад

    Superr

  • @LakshyaprasadRchb
    @LakshyaprasadRchb 2 года назад

    How do I display the details taken in as a table?

  • @harsimerkaur4792
    @harsimerkaur4792 2 года назад

    sorry irrelevant question:
    Which theme are you using?

  • @syedabbas5657
    @syedabbas5657 3 года назад

    How are you saving the user information

  • @MaheshKumar-qy6og
    @MaheshKumar-qy6og 2 года назад

    How to access the updated props in form success component?

  • @oskarolortegui9623
    @oskarolortegui9623 3 года назад

    42:30 - I'm very confuse, where that "callback" comes from and what does it does???

    • @gamelover6508
      @gamelover6508 3 года назад +1

      the callback is come from the parameter. In formsignup we pass this : const FormSignup = ( {SubmitForm} ) => {
      const {handleChange,values,errors,handleSubmit} = useForm(
      SubmitForm, here he is you see
      validataInfo
      )
      when we go to useform . we say this submit form : a callback

  • @abhijeetshikharvlog1444
    @abhijeetshikharvlog1444 Год назад

    Callback function in useEffect how to fix depency aaray

  • @AlexSchwartzATV
    @AlexSchwartzATV 3 года назад +3

    callback is not a function? my page looks exactly like yours. not sure what's happening..

  • @roshanstepit
    @roshanstepit 3 года назад

    Excellent tutorial brother . How can I activate these errors onBlur. Please reply

  • @KhalidKhan-zj5lq
    @KhalidKhan-zj5lq 3 года назад

    in your provided source code where is package.json file bro, i think it is missing..

  • @sultancreator3920
    @sultancreator3920 3 года назад

    how can i create login page in mysql database system

  • @shiddarthbista2248
    @shiddarthbista2248 3 года назад

    Do you know how you can use axios to post the data from the form to a database

    • @briandesign
      @briandesign  3 года назад +2

      I'd recommend checking out some mern stack videos or ones using mongo db/node for forms if you wanna do that

  • @mridul5643
    @mridul5643 2 года назад

    brian can you help me out how can i display username and email when it says we have recieved your request this question was asked to me in the interview

  • @nitinmomaya
    @nitinmomaya Год назад

    can someone please explain that useEffect function part

  • @dimitriywhite7728
    @dimitriywhite7728 3 года назад

    Why from useForm you return object not array?

  • @user-kn4lf4wv4r
    @user-kn4lf4wv4r 3 года назад +5

    Did everything according to your code and this error: React Hook useEffect has missing dependencies: 'callback' and 'isSubmitting'. Either include them or remove the dependency array. If 'callback' changes too often, find the parent component that defines it and wrap that definition in useCallback react-hooks/exhaustive-deps

    • @user-kn4lf4wv4r
      @user-kn4lf4wv4r 3 года назад +1

      useEffect(
      () => {
      if (Object.keys(errors).length === 0 && isSubmitting) {
      callback();
      }
      },
      [errors]
      );

    • @briandesign
      @briandesign  3 года назад

      check my github and see if you mistyped anything

    • @Diasraphael88
      @Diasraphael88 3 года назад

      ​@@user-kn4lf4wv4r in the Form Signup file ({ submitForm }) we missed this ...so this error.. :)

    • @julianb.6580
      @julianb.6580 3 года назад +3

      Hey if you haven't already fixed this, you can fix it by going to your submit handler and wrapping it in a useCallback. I will give you an example using his code.
      So in his Form component he has a submit handler which looks like this:
      function submitForm() {
      setIsSubmitted(true);
      }
      What you want to do is import UseCallback from react and then change the submit handler to be like this:
      const submitForm = useCallback(() => {
      setIsSubmitted(true)
      }, [setIsSubmitted])
      Then also in the custom hook so the useForm component you would want to add the callback and the isSubmitting as a dependancy to the useEffect like this:
      useEffect(() => {
      if (Object.keys(errors).length === 0 && isSubmitting) {
      callback()
      }
      }, [errors, isSubmitting, callback])
      and now it should work :) basically the callback here is referring to the submit function, or thats just my understanding.
      Hope this helps you

  • @Ninjadunet
    @Ninjadunet 3 года назад

    Hi sir, very nice video. I've been struggling with the callback function. I've done everything you've written but can't figure out why, my callback function is undefined when accessed from useForm. It seems that passing the submitForm function as a parameter of my form in the Form.js doesn't work. I've been trying for hours but can't figure out what's the issue. Can some one help me ? Thank you.

    • @brian-lau
      @brian-lau 3 года назад

      you probably forgot to pass submitForm down the chain

    • @Ninjadunet
      @Ninjadunet 3 года назад

      @@brian-lau I've found out few weeks ago ! It's because I called the parameter validateForm and thought it was the function I passed down the chain that I needed to use. But it was the parameter. They had a different name. Now it works ! Thanks for trying to help.

  • @imranshah4494
    @imranshah4494 3 года назад

    Hi Sir, could you make a video of sending email contact form.Also, please apply validations......

  • @syedawaisalishah9435
    @syedawaisalishah9435 3 года назад

    i have question is there are necceray for me to become expert in js for learning react and how many js i will learn first?

    • @briandesign
      @briandesign  3 года назад +3

      just build 10 javascript projects and then try to follow a react course. If you notice that nothing makes sense, then you should go back to learn more javascript.

  • @nilufer9423
    @nilufer9423 3 года назад

    I wan to create login page too and look if the values of signup and login is same. How can ı access the values of signup?

  • @jpactor155
    @jpactor155 3 года назад

    WHAT is his VSC theme??? Anyone know?

  • @MDMAINULISLAM-mr6vg
    @MDMAINULISLAM-mr6vg 3 года назад

    I liked it...please make videos also of react-typescript also....By the way can I get a help from you if needed?Then,how?

  • @roshanstepit
    @roshanstepit 3 года назад

    Great video. How can i achieve this if i want the validations to run onchange too :-D

  • @Khalmadrog
    @Khalmadrog 3 года назад +2

    Nice tutorial! I followed on my project but I've got a 'callback is not a function' error message when I submit the form, and I don't know why :(

    • @xiaoshanli8468
      @xiaoshanli8468 3 года назад +1

      I got the same issue, how do you fix it?

    • @Khalmadrog
      @Khalmadrog 3 года назад

      @@xiaoshanli8468 Make sure you have scheduler version 14 or more on your pakage.json dependencies, that made it for me

    • @DineshRN751
      @DineshRN751 2 года назад

      Change it to a regular function instead of arrow function.. that solved my issue..