useActionState - React's Latest Critical Hook (Next.js + Server Actions)

Sdílet
Vložit
  • čas přidán 7. 09. 2024

Komentáře • 106

  • @ByteGrad
    @ByteGrad  Před 2 měsíci +7

    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 Před měsícem

      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 Před 2 měsíci +39

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

  • @jamj2000
    @jamj2000 Před 2 měsíci +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

  • @tjblackman08
    @tjblackman08 Před 2 měsíci +9

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

    • @joshuadeguzman2911
      @joshuadeguzman2911 Před 2 měsíci +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.

  • @GrahamBillington
    @GrahamBillington Před 2 měsíci +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 Před 2 měsíci +3

    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 Před 2 měsíci +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! 🙏🏼

  • @theintjengineer
    @theintjengineer Před 2 měsíci +4

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

  • @santiskiffa
    @santiskiffa Před 2 měsíci +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.

  • @mahmoudfarghly2970
    @mahmoudfarghly2970 Před 2 měsíci +11

    This hook is way more better than useFormState

    • @daytatech-youtube
      @daytatech-youtube Před 2 měsíci

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

    • @mahmoudfarghly2970
      @mahmoudfarghly2970 Před 2 měsíci

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

  • @EliBates95
    @EliBates95 Před 2 měsíci +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.

  • @PrajwalSingh-ge1ke
    @PrajwalSingh-ge1ke Před měsícem +1

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

  • @juliusgrisette
    @juliusgrisette Před 2 měsíci +1

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

  • @user-zt7oz4ur3l
    @user-zt7oz4ur3l Před 2 dny +1

    useActionState love it!!

  • @theintjengineer
    @theintjengineer Před 2 měsíci +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!

  • @PalladinGOLD
    @PalladinGOLD Před 23 hodinami

    @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?);

  • @suman_js495
    @suman_js495 Před 2 měsíci +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.

  • @sweethouse2773
    @sweethouse2773 Před 2 měsíci +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 Před 2 měsíci

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

  • @madmaxdev
    @madmaxdev Před 2 měsíci +1

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

  • @dusanknezevic4028
    @dusanknezevic4028 Před 2 měsíci +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 Před 2 měsíci

      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 Před měsícem

      @@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 :)

  • @julienheng3880
    @julienheng3880 Před 2 měsíci +1

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

  • @jy76312
    @jy76312 Před měsícem +1

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

  • @mrbjjackson
    @mrbjjackson Před 2 měsíci +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.

  • @tarcisioandrade9816
    @tarcisioandrade9816 Před 2 měsíci

    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"

  • @avigdev
    @avigdev Před 2 měsíci +3

    Very clear.thanks!

  • @dejoma.
    @dejoma. Před 2 měsíci

    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) ?

  • @AllysonTS
    @AllysonTS Před 2 měsíci +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? 😂

  • @sajarmohammed8045
    @sajarmohammed8045 Před 2 měsíci

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

  • @marroos5440
    @marroos5440 Před 2 měsíci +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.

  • @herozero777
    @herozero777 Před 22 dny

    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?

  • @alexdin1565
    @alexdin1565 Před měsícem

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

  • @ruanxun9454
    @ruanxun9454 Před 27 dny

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

  • @Michaeltje01
    @Michaeltje01 Před 2 měsíci

    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.

  • @xzex2609
    @xzex2609 Před 2 měsíci

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

  • @bratislavZdravkovic98
    @bratislavZdravkovic98 Před 2 měsíci +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?

  • @EnesKab
    @EnesKab Před měsícem

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

  • @reactjs1900
    @reactjs1900 Před 2 měsíci +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 Před 2 měsíci

      same here

    • @reactjs1900
      @reactjs1900 Před 2 měsíci

      @@XITAXB3AT use dynamic import and use client both

    • @XITAXB3AT
      @XITAXB3AT Před 2 měsíci

      @@reactjs1900 can you share a snippet of code?

  • @nithinn2632
    @nithinn2632 Před měsícem

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

  • @ismailtirmizi
    @ismailtirmizi Před 11 dny

    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?

  • @ianc6418
    @ianc6418 Před 2 měsíci +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  Před 2 měsíci +1

      Still RC, but should be stable soon

  • @FaizKhan-jk9po
    @FaizKhan-jk9po Před 2 měsíci

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

  • @kashmirtechtv2948
    @kashmirtechtv2948 Před 2 měsíci

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

  • @troquelina
    @troquelina Před 2 měsíci

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

  • @doppeltevier
    @doppeltevier Před 2 měsíci

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

  • @easy_frontend
    @easy_frontend Před dnem

    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 Před 23 hodinami

      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).

  • @sawos1596
    @sawos1596 Před 2 měsíci

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

  • @aymenbachiri-yh2hd
    @aymenbachiri-yh2hd Před měsícem +1

    Thank you so much

  • @MattTheMotorbike
    @MattTheMotorbike Před 2 měsíci

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

  • @kingston7151
    @kingston7151 Před 2 měsíci

    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 Před 2 měsíci

    Thanks love your teaching style

  • @excalibur2417
    @excalibur2417 Před 2 měsíci +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  Před 2 měsíci +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 Před 2 měsíci

      @@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

  • @wellbrutin
    @wellbrutin Před měsícem +1

    thanksss

  • @eliamzg
    @eliamzg Před 2 měsíci +2

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

  • @Monstermash355
    @Monstermash355 Před 2 měsíci

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

  • @AleRovella
    @AleRovella Před 2 měsíci +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  Před 2 měsíci +2

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

    • @AleRovella
      @AleRovella Před 2 měsíci +1

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

  • @meka4996
    @meka4996 Před 2 měsíci +1

    Amazing!

  • @protonys
    @protonys Před 2 měsíci

    it is work only in the canary version nextjs

  • @mrlectus
    @mrlectus Před 2 měsíci

    so why is previous state typed as any instead of number

  • @nasko235679
    @nasko235679 Před 2 měsíci

    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  Před 2 měsíci

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

  • @paw565
    @paw565 Před 2 měsíci

    What about optimistic updates?

  • @tristandg
    @tristandg Před 2 měsíci

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

  • @user-nz8gk3wp5x
    @user-nz8gk3wp5x Před 2 měsíci +1

    it's very helpful

  • @olusanyaolamide9764
    @olusanyaolamide9764 Před 2 měsíci

    Thank you Brad

  • @kashmirtechtv2948
    @kashmirtechtv2948 Před 2 měsíci

    great

  • @lukq90
    @lukq90 Před 18 dny

    We returned to react query

  • @abhishek427821
    @abhishek427821 Před 2 měsíci

    Can we use this with redux?

  • @kassiogluten
    @kassiogluten Před 25 dny

    canary version only ?

  • @ibrvimv
    @ibrvimv Před 2 měsíci

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

  • @phucoan9098
    @phucoan9098 Před 2 měsíci

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

    • @ByteGrad
      @ByteGrad  Před 2 měsíci

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

  • @yj4105
    @yj4105 Před 2 měsíci

    what about useTransition?

  • @0xtz_
    @0xtz_ Před 2 měsíci

    thats cool 😎
    but still sticking with trpc for production hhhh

  • @AndrewFloatrx
    @AndrewFloatrx Před 2 měsíci

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

  • @indimerz
    @indimerz Před měsícem

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

    • @ByteGrad
      @ByteGrad  Před měsícem +1

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

    • @indimerz
      @indimerz Před měsícem

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

    • @maxrusakovic
      @maxrusakovic Před měsícem

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

  • @mDHARYL
    @mDHARYL Před 2 měsíci

    Wow

  • @CadisDiEtrama000
    @CadisDiEtrama000 Před 2 měsíci +2

    ReactQuery much? 😂😂

    • @mrlectus
      @mrlectus Před 2 měsíci

      Pagination, infinitQuery ...

  • @kurwacherry
    @kurwacherry Před 2 měsíci

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

    • @ByteGrad
      @ByteGrad  Před 2 měsíci +1

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

  • @MrFinchBen
    @MrFinchBen Před 2 měsíci +1

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

  • @DmGs-h1i
    @DmGs-h1i Před 2 měsíci

    it is work only in canary version nextjs