useActionState - NEW in React 19 / Next.js 15 - Critical Hook for Server Actions

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

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

  • @ByteGrad
    @ByteGrad  5 месяцев назад +8

    Hi, my latest course is out now (Professional React & Next.js): bytegrad.com/courses/professional-react-nextjs -- I'm very proud of this course, my best work!
    I'm also a brand ambassador for Kinde (paid sponsorship). Check out Kinde for authentication and more bit.ly/3QOe1Bh

    • @jannickpedersen4620
      @jannickpedersen4620 4 месяца назад

      Have you done a fullstack with Kinde :D? Yeah I like Kinde too, I have an E-Commerce project in NextJS, Stripe and Kinde ect :D

  • @judemusyoki7052
    @judemusyoki7052 5 месяцев назад +39

    You contributed so much to my Nextjs knowledge in the last year, thanks for the good work!

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

      Happy to help!

  • @_tanzil_
    @_tanzil_ 2 месяца назад

    In case you are wondering what if you wanna do some client side validations before calling the "action" of the useActionState hook, just watch carefully this part 11:00 and you will understand what to do. 🙂
    ByteGrad, you just made everything clear to me. ❤

  • @jamj2000
    @jamj2000 5 месяцев назад +7

    Thanks for this great video.
    I suggest it is more meaning to write
    const [ result, action, isPending ] = useActionState ( createTask, null )
    It works for all cases, not only errors

  • @GrahamBillington
    @GrahamBillington 5 месяцев назад +2

    Absolute legend. I ran into this friction when actions first dropped, and this is the exact solution I was looking for. You've been so helpful with the new next update

  • @Khalid-ji6rs
    @Khalid-ji6rs 5 месяцев назад +4

    I think a good use case for previousState is for example tracking how many times wrong credentials have been submitted and block user from trying to login after 3 or 4 or 5 attempts for example

  • @bobbyboxer2664
    @bobbyboxer2664 5 месяцев назад +3

    Thank so so much for all your help. You’ve made my transition to next a hell of a lot easier. Specifically your form related videos! 🙏🏼

  • @anangyoga1381
    @anangyoga1381 2 месяца назад +1

    Hi, thanks for the explanation. now I understand the difference between useFormState & useActionState. Also thank you for giving us example of using useActionState that is not on the Form.

  • @tjblackman08
    @tjblackman08 5 месяцев назад +9

    Love the pending state! Only thing I need now is a reset method.

    • @joshuadeguzman2911
      @joshuadeguzman2911 5 месяцев назад +1

      I relate to this comment too much 😂. Jack Herrington did this, too. You can still benefit from progressive enhancement but still have client-side validation and can reset form.

  • @EliBates95
    @EliBates95 5 месяцев назад +2

    Excellent video! For me personally I think this is great for small use cases, but it feels very much not production ready... It leaves a lot to be desired versus things like react hook form, zod, and react query/trpc.

  • @juliusgrisette
    @juliusgrisette 5 месяцев назад +2

    Thank you so much. Maybe future video on useOptimistic as well?
    Official docs are very dense

  • @PrajwalSingh-ge1ke
    @PrajwalSingh-ge1ke 4 месяца назад +1

    Great video! Please make a video on how to use useActionState along with zod validations

  • @santiskiffa
    @santiskiffa 5 месяцев назад +3

    thanks for all your vids, they are so helpful!!
    would love to see something on testing a nextjs app with jest (especially the part where you load data from your database)... struggling a lot and they ask that often when applying for jobs.

  • @КириллАмиров-ц2ф
    @КириллАмиров-ц2ф Месяц назад +1

    the best teacher out there. thank you 🙏

  • @marroos5440
    @marroos5440 5 месяцев назад +1

    At 0:30 i started writing comment when to use useActionState and when useFormState but couple seconds later there was explanation. First watch then ask is good rule lol :-) UseActionState looks very handy. Ty for great explanation with examples.

  • @mahmoudfarghly2970
    @mahmoudfarghly2970 5 месяцев назад +12

    This hook is way more better than useFormState

    • @daytatech-youtube
      @daytatech-youtube 5 месяцев назад

      agreed dealing with pending status in an external component was kindof clumsy

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

      @@daytatech-youtube + using useFormState to retreive data from the server was also annoying

  • @Grishopping
    @Grishopping Месяц назад +1

    Thanks for the video, do you have any video that talks about how to use next.js + turborepo + prisma to translate the above into English?

  • @theintjengineer
    @theintjengineer 5 месяцев назад +1

    Wesley, on another note (2 actually, haha):
    - Do you cover all that `useActionState()` thingy on your React|Next.js Course or is that a feature that was released after the course launch?
    - Would you mind creating a small video using Drizzle ORM? It can even be this very same Tasks Project, just using Drizzle instead. And add, say, a `title` field to the `Task` entity, and a, say, check box for a `isCompleted` field, just so it's not exactly the same thing project.😅
    Thank youuu!

  • @suman_js495
    @suman_js495 5 месяцев назад +1

    Hey dude, love your content. Please use"import server only". It will make sure that your server code does slip into the client side.

  • @eneiarestivan
    @eneiarestivan Месяц назад

    i have this warning when use outside forms: deleteButton.tsx:31 An async function was passed to useActionState, but it was dispatched outside of an action context. This is likely not what you intended. Either pass the dispatch function to an `action` prop, or dispatch manually inside `startTransition`

  • @Bratislav_Zdravkovic
    @Bratislav_Zdravkovic 5 месяцев назад +2

    If we receive an error from a server action and we want to show it using a notification (toast), should we use useEffect for that?

  • @julienheng3880
    @julienheng3880 5 месяцев назад +1

    Thank you very much for the tutorial!
    But how do you reset the form?

  • @박헌남
    @박헌남 3 месяца назад +1

    useActionState love it!!

  • @avigdev
    @avigdev 5 месяцев назад +3

    Very clear.thanks!

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

    @ByteGrad Why do you use 3 return value from useActionState (video uploaded 2 month ago). In official docs: const [state, formAction] = useActionState(fn, initialState, permalink?);

  • @theintjengineer
    @theintjengineer 5 месяцев назад +4

    This guy's a Master of his Craft.
    Thanks, Wesley.

  • @dusanknezevic4028
    @dusanknezevic4028 5 месяцев назад +1

    Hey Wesley, another great and on-time explainer. In your opinion, what is the use case for react-hook-form and even react query when we have all these functionalities already available in next.js that can do most of what those libraries are meant for?
    It would be a gem of a video if you could integrate this new information together with Zod and the best practices approach to dealing with forms in React/Next.js. like I remember you did a few times in the past.
    Cheers!

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

      I understand why you mentioned React Query but not React hook form. What is the functionality of Next.js that can replace it?

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

      @@jihefel92 You already have a native browser validation (google Client Side Form Validation MDN), which is quite sufficient react-hook-form replacement in this scenario where you make the full scale server side validation using the useActionState and ZOD. In this scenario I think adding extra client side library is overload, and also may pose some performance issues as well. Off course I may be wrong on this, that is why I posted this question in the first place :)

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

      @@dusanknezevic4028 hey tell me what answer you got.

  • @AllysonTS
    @AllysonTS 5 месяцев назад +1

    Wesley this is really awesome. Do you know if this works with a onSuccess to do something with the form? Like the form is in a modal/dialog, after submiting maybe i want to close the modal, and on error maybe show error like u did. Do u know is already possible with serverActions/actionState/transition/formState or whatever they name now days? 😂

  • @mrbjjackson
    @mrbjjackson 5 месяцев назад +1

    Thanks for this video. One thing I've noticed is that if you use useActionState to generate your "action" function to run, it is no longer asynchronous. I want to trigger the action, await the result and then run some other code but I am unable to do this... Any idea how we can achieve this? It works fine if I use the raw version of the server action.

  • @madmaxdev
    @madmaxdev 5 месяцев назад +1

    How about we do form validation in actions and return validation errors?

  • @sweethouse2773
    @sweethouse2773 5 месяцев назад +1

    @ByteGrad I really wish you will do a full next js tutorial. No next js backend. Just the frontend interacting with an external backend, state management if applicable and more

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

      just use Zustand for state management. It simplifies things a whole lot

  • @paw565
    @paw565 5 месяцев назад +1

    What about optimistic updates?

  • @jy76312
    @jy76312 5 месяцев назад +1

    Can i please know what react and next version you are using

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

    Thank you for introducing useActionState, cheers. What's the benefit for this over a traditional endpoint with an HTTP status code?
    When you have to repeat the interpretation of state/error about 5 times, I immediately get the feeling something smells iffy.
    You give a good introduction of the functionality but it does not put it in context properly. I would never want to put this in production.

  • @aymenbachiri-yh2hd
    @aymenbachiri-yh2hd 5 месяцев назад +1

    Thank you so much

  • @ruanxun9454
    @ruanxun9454 4 месяца назад

    What's the use case of useActionState and react-query I am wondering? Any advantage of using either of these?

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

    Do you have the repository for this new exciting thing you so clearly explained? Thanks!

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

    Using useActionState with useTransition is a good approach ? Or is it not always necessary ?
    Thank you in advance 🙏

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

    When will this feature be available in a stable release? I tried to build the Next.js canary and it gives error. I wanted to use this feature in production, any advice in that regard?

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

    How about a getRequest, where you get the data? I have quite some server actions that just 'get data'. That would then be the state? I am wondering why you didn't mention this; maybe I misunderstand the primary use of serverActions; could you elaborate why you don't use serverActions to get Data (and then specifically with this new hook) ?

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

    For the delete task part could you not of used an action instead of the onClick?

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

    what do you use to record your videos, i see that it automatically cuts out parts when you aren't speaking that's so cool

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

    Thanks love your teaching style

  • @excalibur2417
    @excalibur2417 5 месяцев назад +1

    That's pretty cool. Do you happen to know if they will add this to nextjs 14 as well? I don't really wanna mess with next 15 for now

    • @ByteGrad
      @ByteGrad  5 месяцев назад +1

      I’m using Next.js 15 RC in the video. V15 is not hugely different from V14 so upgrading shouldn’t be a big issue

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

      @@ByteGrad When you have a big project with tons of api routes that will all go from default cached to dynamic it is kind of a big change. I also saw that React 19 is currently extremely slow inside of suspense waterfalls and I use suspense a lot in my apps

  • @AleRovella
    @AleRovella 5 месяцев назад +1

    could you tell me wich version of react and nextjs are you using? because im having an error when i want to implement it: TypeError: (0 , react__WEBPACK_IMPORTED_MODULE_1__.useActionState) is not a function or its return value is not iterable

    • @ByteGrad
      @ByteGrad  5 месяцев назад +2

      Yeah I’m using Next 15RC in the video, should be stable soon

    • @AleRovella
      @AleRovella 5 месяцев назад +1

      @@ByteGrad i want this now! jajaja, it's a better approach than useFormState

  • @reactjs1900
    @reactjs1900 5 месяцев назад +1

    Getting 'Reference error : document is not defined' , cause i am using one outside npm package (client side) in my nextjs 14 app. Tried to make component client side by using "use client" also. This is probably because of nextjs prerenders everything in server. How to fix it. Please help. commenting here cause this is the latest video

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

      same here

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

      @@XITAXB3AT use dynamic import and use client both

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

      @@reactjs1900 can you share a snippet of code?

  • @meka4996
    @meka4996 5 месяцев назад +1

    Amazing!

  • @MusadiqKhan
    @MusadiqKhan Месяц назад

    @byteGrad - Can you create a video where you talk about redirects in server actions. For example, I have login form and I want to redirect to dashboard

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

    My state is returning undefined when I put the "use server" directive in the action, when I remove the start it returns normal, but I can't use the "cookies" method, as it doesn't have the "use server"

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

    How can i clear my input fields after submitting the form using server action?

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

    Are server actions "just functions that run on the server" though? Is making a server action with the use server directive the same as executing a function in your server component? AFAIK server actions are only POST requests. What if I use a server action to fetch data from my database? OR what if I just make a helper function inside a separate file that does not have the "use server" directive and then I fetch the data directly in the server component by calling that function, is that the same as calling a server action?

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

      Yeah they’re more than just functions on the server. They essentially open a POST API endpoint on the server

  • @CarlosIsaacRSison
    @CarlosIsaacRSison Месяц назад

    It's been days of researching and I still can't find a tutorial that tackles with the useActionState resetting all inputs, maybe you have a way to disable it? Because if the user made a mistake in 1 field all other fields will reset which is not a good user experience

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

    I'm trying to use this for deployment but when I do `npm run build` I get error that React doesn't have the hook 'useActionState'. Why is this?

  • @ianc6418
    @ianc6418 5 месяцев назад +1

    Is this the latest stable release or is it just in the release candidate? I am learning NextJS and don't mess with RC because it hinders my learning progress.

    • @ByteGrad
      @ByteGrad  5 месяцев назад +1

      Still RC, but should be stable soon

  • @nithinn2632
    @nithinn2632 4 месяца назад

    but form is getting cleared after submition
    but i need those prev values to be there only?

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

    Thank you Brad

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

    I am confused 😕 , in documents useAcrionState returns two value , state and Action , but you have a pending too

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

    couldnt get it working on my nextjs project, do i need to update to a higher nextjs version? or use experimental?

    • @ByteGrad
      @ByteGrad  5 месяцев назад +1

      It will work in Next 15, I’m using Next 15 RC in the video

  • @alexdin1565
    @alexdin1565 4 месяца назад

    please can you make a video about Search and Pagination using server?

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

    just starting the video but isnt it the same as using action with useMutation ?

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

    Could you create a video about Atlassian's new framework-agnostic, low-level dnd library called "pragmatic-drag-and-drop"?

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

    Hello , if i'm using server actions with the useActionState how do i return a custom response for the backend
    do i do it also in the action file or i create api/router i'm really confused

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

      I was also confused, in the documentation it says, for mutations (update, insert) use server actions, but to retrieve data use fetching or nextjs api handler(route handler).

  • @AlexanderBelov-y8o
    @AlexanderBelov-y8o 2 месяца назад

    Can I return a success message from the action?

  • @MicroViewers-j1s
    @MicroViewers-j1s 5 месяцев назад +1

    it's very helpful

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

    CREATE A VIDEO COVERING ALL ESSENTIAL CONCEPTS OF NEXT JS AND THOSE THAT WILL HELP US IN REAL PROJECTS

  • @wellbrutin
    @wellbrutin 4 месяца назад +1

    thanksss

  • @FaizKhan-jk9po
    @FaizKhan-jk9po 5 месяцев назад

    Can anybody tell me the name of vs code theme that ByteGrad currently using?

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

    how to activate support for it ? i get an error message

  • @EnesKab
    @EnesKab 4 месяца назад

    PreviousState would be useful for "undo" operation right after user edited something.

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

    Can we use this with redux?

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

    so why is previous state typed as any instead of number

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

    what about useTransition?

  • @kassiogluten
    @kassiogluten 4 месяца назад

    canary version only ?

  • @DmGs-h1i
    @DmGs-h1i 5 месяцев назад

    it is work only in canary version nextjs

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

    thats cool 😎
    but still sticking with trpc for production hhhh

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

    We returned to react query

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

    great

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

    i cant import useActionState from react like u, i dont know why

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

      Because this is in version 15 RC, it’s not stable yet

  • @eliamzg
    @eliamzg 5 месяцев назад +2

    How dare you get sponsorship from an actual useful and related service and make the ad something useful to boot?

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

    I just watched: "No - /API, Yes - Server Actions".
    Next video reccomended: "No - Server Actions, Yes - useActionState" 😅

  • @indimerz
    @indimerz 4 месяца назад

    i think this hook is still experimental. i cant import it by just entering its name.

    • @ByteGrad
      @ByteGrad  4 месяца назад +1

      I'm using Next 15RC in the video, that's why it works for me

    • @indimerz
      @indimerz 4 месяца назад

      @@ByteGradohk, can we upgrade project from next 14 to 15 with single command?

    • @maxrusakovic
      @maxrusakovic 4 месяца назад

      @@ByteGrad Please, mention it in description or video name =)

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

    Wow

  • @A.Floatrx
    @A.Floatrx 5 месяцев назад

    useTransition also helps display pending status during server action execution….

  • @CadisDiEtrama000
    @CadisDiEtrama000 5 месяцев назад +2

    ReactQuery much? 😂😂

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

      Pagination, infinitQuery ...

  • @MrFinchBen
    @MrFinchBen 5 месяцев назад +1

    Let me put tutorial into tutorial, so you have tutorial in your tutorial :D

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

    it is work only in the canary version nextjs