This is why useFormState and useFormStatus are important

Поделиться
HTML-код
  • Опубликовано: 10 янв 2025

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

  • @sylarfx
    @sylarfx Год назад +6

    zod has parseSafe or safeParse function that returns object with success or errors, so you don't have to do try catch

  • @SyntaxLexx
    @SyntaxLexx Год назад +4

    Clean!
    I'd like a follow-up on using this with Shadcn-UI and react-hook-form

  • @codinginflow
    @codinginflow 9 месяцев назад

    But for required field, even without JavaScript, you would just use the browser's required option and safe the server roundtrip, right?

  • @tictac1020
    @tictac1020 6 месяцев назад

    Thanks for the well explained video, dude. I'm at the tail end of Brad Traversy's Next 14 course and doing some refactoring before calling it 'done'. I cannot get useFormState to work right AT ALL. It's driving me nuts and I'm doing it just like you showed, how it was shown in the course's project demo code, and how I see it elsewhere. First time the form submits, the formState just gets set to the initial value. Second time, works normally. If anyone has any ideas, I'm at my wit's end. Doesn't seem like ANYONE ELSE has run into this, which usually means I'm missing a semicolon or have a typo but I've been over it with a fine toothed comb and it WORKS but only after the first submission.

  • @philheathslegalteam
    @philheathslegalteam Год назад +12

    I like Next. I like nested layouts. I like BFF functions. I hate instability. I just want one question answered by Vercel. Which person thought it would be a good idea to change the order of arguments for a server action depending on if it's used inside useFormState or action={}?
    Seriously... That is perhaps the worst API decision I have ever seen from Next.

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

      I think they wanted the interface to be like reducer, because it’s kind of acting like a reducer if you look harder at what it’s doing

  • @filipkovac767
    @filipkovac767 Год назад +4

    How would you do client side validated form using server actions? Onblur field validation is pretty common.
    Plus how would you do nested array fields array->object->string. I am sure it is possible to recreate the structure using native elements, but it would be a great content for RUclips. Basically how to replace react-hook-form with server actions, if it is even possible. If not then how to combine them nicely.

    • @godfather7339
      @godfather7339 8 месяцев назад

      wouldn't you use useEffect?

  • @real-oppenheimer
    @real-oppenheimer Год назад +3

    I really don't like that the input validation is ONLY done on the server. You'll always have to wait for the server response to know your field is valid. What if it is invalid (e.g. "min. 2 characters"), you add some characters to make it valid? Without client-side validation, you only see your validation status after a submit request - and I bet you don't want to send a request for every keystroke. Sure, you have to validate in the server action anyways, but I have yet to see a solution for how to ALSO do the validation on the client (especially for re-validation on input) with REact's new form hooks.

    • @WebDevCody
      @WebDevCody  Год назад +1

      For the best ux, you’d want validation on the server and the client, but in many cases server validation alone gets the job done and keeps your code simplier.

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

      There is also simple validation in the browser without JS, like input type email, URL, etc.

  • @Balance-8
    @Balance-8 Год назад +2

    Can you please make the repo available to viewed?

  • @ankitkumarjat9886
    @ankitkumarjat9886 Год назад +9

    We are using remixjs at my company, and it has all these functionality for years without server components. And more importantly it is stable. I am not saying that nextjs is bad.

    • @samiullahsheikh5015
      @samiullahsheikh5015 9 месяцев назад

      We should say nextjs is highly unstable and full of bugs... They market reactjs canary version as nextjs own production ready features

    • @ekberehmedzade4768
      @ekberehmedzade4768 5 месяцев назад

      But remix has not SSG and ISR every page recreates every fetch I am not saying remix is bad

    • @ankitkumarjat9886
      @ankitkumarjat9886 5 месяцев назад

      @@ekberehmedzade4768 you don't need that just cache it with a cdn and you get same functionality as ISR and SSG.

    • @cant_sleeeep
      @cant_sleeeep 3 месяца назад

      you mean remixXx?

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

    It comes to deciding the advantages of validation on input blur vs js disabled. I guess it’s too valuable to show immediate feedback to user in large forms. This could be pretty neat trick if these hooks would solve it as react hook forms does. Thanks for the video, I wasn’t aware of these hooks.

  • @eviltables7235
    @eviltables7235 Год назад +3

    Does this replace using libraries like react hook forms?

    • @WebDevCody
      @WebDevCody  Год назад +1

      I think so because I don’t think react hook forms will work with JavaScript disabled. Again, maybe that’s not important to your app

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

      Cool, really interesting approach for that use case @@WebDevCody

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

      React Hook Forms requires the spread operator for the ...Register method so yes, need JS. @@WebDevCody

  • @srkuleo
    @srkuleo 10 месяцев назад

    My only problem with this approach is binding users ID. How would you pass the ID to server action in case application has more users and data is stored inside db?

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

    Knew this video was coming since that twitter post! lol awesome stuff bro.

  • @SeibertSwirl
    @SeibertSwirl Год назад +1

    Great job Love!

  • @huynguyenquoc3692
    @huynguyenquoc3692 3 месяца назад

    I wonder why how can i clear field error after continue to typing on input ?

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

    I can't repro how you got the form to return errors with no JavaScript. Seems like no way to get returned data from server actions into RSC. Possibly you are using an obsolete build? Also, useFormState params seem to have changed from your example too. If you have repo link, please post.

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

      I don't think I added this code to any repo. Some how useFormState persists information between the server and client which allows you to know when to show or hide errors.

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

      @@WebDevCody but... doesn't useFormState only work in a client component? (which means JavaScript?)

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

      @@SiliconValleyCodeCampVideos it seems to work fine when you turn off JavaScript. I think it’s because a client component gets one pass through during the server render, so it’s able to hydrate react state on the initial load

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

    Can I do this soluton without server actions, inside an api route handler?

  • @muhammadshoaib1838
    @muhammadshoaib1838 11 месяцев назад

    These errors should be visible only after the form is submitted but if someone wanna add the functionality like in formik when user type fields starts showing errors. it could be achieve?

  • @movietime8403
    @movietime8403 10 месяцев назад

    where I can find the validation codes of zod here ?

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

    - what react and react-dom version are you on?
    - what next version?
    - source code?

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

    Is this code in github? I saw your github link is pointing to the root, not to this specific code ( which is extremely useful and I would like to download it )

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

    Is this form stuff with server actions important to know? Me using "use client" with my good old handlesubmit with react hook form is still working 😊😀. Its cool having the app work without Javascript but what use case would my app ever have for that, none.

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

      if your app never has a requirement of "no javascript support", then yeah, use client all the way

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

    I don't know, do we really need to send the client input to the server for validation??
    I mean, we are going to depend on the network just to tell the user that the password must be at least 9 characters?

    • @WebDevCody
      @WebDevCody  Год назад +1

      The server should always validate all input from users

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

      @@WebDevCody yes you are right, I don't think I express myself right.
      I'm just saying that with this paradigm our app will be making some, to my understanding, unnecessary network calls.

  • @usmansbk
    @usmansbk 4 дня назад

    React + "Javascript disabled" doesn't feel normal for some reason.

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

    is this solution better / faster than using react-hook-form ?

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

      React-hook-form won't work without JS. But overall, RHF is still better IMO.

    • @muhamadizranovan1653
      @muhamadizranovan1653 23 дня назад

      @@codinginflow but can we use RHF with server action?

    • @codinginflow
      @codinginflow 23 дня назад

      @@muhamadizranovan1653 Yes

  • @demian.succs0
    @demian.succs0 Год назад

    shouldnt you use 'use server' on the server action???

    • @WebDevCody
      @WebDevCody  Год назад +1

      you can either have the "use server" at the top of the entire file, or you can put it inside the functions

    • @demian.succs0
      @demian.succs0 Год назад

      @@WebDevCody ohhh didnt know that, ty, great vide tho

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

    This is awesome, make a lib out of it

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

    idk if they changed the versions and didn;t update teir docs but you example is not working for me neither are the docs , nextjs is trash sometimes

    • @samiullahsheikh5015
      @samiullahsheikh5015 9 месяцев назад

      It's just the marketing and hype most of the times..... NextJS is full trash...
      Remix is much and for mist suitable option or u should try something outside of react like vue/nuxtJS

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

    Thank you Sir, for your excellent React videos. I especially appreciate your explanations of useFormState and useFormStatus. They have helped me to better understand and implement React forms.

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

    7:39 I don't want to be an old man. But this is really basic stuff in PHP.

  • @klevert7287
    @klevert7287 3 месяца назад +1

    pain chatgpt doesn't know useFormstate exists