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

  Рет қаралды 31,817

ByteGrad

ByteGrad

Күн бұрын

Пікірлер: 121
@ByteGrad
@ByteGrad 6 ай бұрын
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 5 ай бұрын
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 6 ай бұрын
You contributed so much to my Nextjs knowledge in the last year, thanks for the good work!
@ByteGrad
@ByteGrad 6 ай бұрын
Happy to help!
@_tanzil_
@_tanzil_ 3 ай бұрын
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. ❤
@McFlurriez
@McFlurriez Ай бұрын
mind sharing?
@GrahamBillington
@GrahamBillington 6 ай бұрын
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
@anangyoga1381
@anangyoga1381 3 ай бұрын
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.
@jamj2000
@jamj2000 6 ай бұрын
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
@bobbyboxer2664
@bobbyboxer2664 6 ай бұрын
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! 🙏🏼
@PrajwalSingh-ge1ke
@PrajwalSingh-ge1ke 5 ай бұрын
Great video! Please make a video on how to use useActionState along with zod validations
@juliusgrisette
@juliusgrisette 6 ай бұрын
Thank you so much. Maybe future video on useOptimistic as well? Official docs are very dense
@tjblackman08
@tjblackman08 6 ай бұрын
Love the pending state! Only thing I need now is a reset method.
@joshuadeguzman2911
@joshuadeguzman2911 6 ай бұрын
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.
@КириллАмиров-ц2ф
@КириллАмиров-ц2ф Ай бұрын
the best teacher out there. thank you 🙏
@Khalid-ji6rs
@Khalid-ji6rs 6 ай бұрын
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
@santiskiffa
@santiskiffa 6 ай бұрын
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.
@EliBates95
@EliBates95 6 ай бұрын
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.
@avigdev
@avigdev 6 ай бұрын
Very clear.thanks!
@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`
@webdevmix
@webdevmix Сағат бұрын
I am getting the same error. This applies to 11:00 "useActionState outside of form". I believe the error happens because the action is called from onClick. Would be great if @ByteGrad could provide a solution! Seems he overlooked it in the video.
@julienheng3880
@julienheng3880 6 ай бұрын
Thank you very much for the tutorial! But how do you reset the form?
@Bratislav_Zdravkovic
@Bratislav_Zdravkovic 6 ай бұрын
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?
@supermario6397
@supermario6397 6 ай бұрын
yes
@marroos5440
@marroos5440 6 ай бұрын
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.
@madmaxdev
@madmaxdev 6 ай бұрын
How about we do form validation in actions and return validation errors?
@kartikkaushik4743
@kartikkaushik4743 6 ай бұрын
Thanks love your teaching style
@theintjengineer
@theintjengineer 6 ай бұрын
This guy's a Master of his Craft. Thanks, Wesley.
@ByteGrad
@ByteGrad 6 ай бұрын
Enjoy!
@suman_js495
@suman_js495 6 ай бұрын
Hey dude, love your content. Please use"import server only". It will make sure that your server code does slip into the client side.
@Grishopping
@Grishopping Ай бұрын
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?
@paw565
@paw565 6 ай бұрын
What about optimistic updates?
@jy76312
@jy76312 5 ай бұрын
Can i please know what react and next version you are using
@박헌남
@박헌남 3 ай бұрын
useActionState love it!!
@MattTheMotorbike
@MattTheMotorbike 6 ай бұрын
For the delete task part could you not of used an action instead of the onClick?
@theintjengineer
@theintjengineer 6 ай бұрын
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!
@nithinn2632
@nithinn2632 5 ай бұрын
but form is getting cleared after submition but i need those prev values to be there only?
@mrbjjackson
@mrbjjackson 6 ай бұрын
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.
@MusadiqKhan
@MusadiqKhan 2 ай бұрын
@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
@AllysonTS
@AllysonTS 6 ай бұрын
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? 😂
@aymenbachiri-yh2hd
@aymenbachiri-yh2hd 5 ай бұрын
Thank you so much
@RitikRaj-we2sc
@RitikRaj-we2sc 8 күн бұрын
When combined with react-hook-form and zod, the client side validation doesn't work.
@dusanknezevic4028
@dusanknezevic4028 6 ай бұрын
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 6 ай бұрын
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.
@sweethouse2773
@sweethouse2773 6 ай бұрын
@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 6 ай бұрын
just use Zustand for state management. It simplifies things a whole lot
@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?);
@ruanxun9454
@ruanxun9454 4 ай бұрын
What's the use case of useActionState and react-query I am wondering? Any advantage of using either of these?
@tfandrad3
@tfandrad3 6 ай бұрын
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"
@RitikRaj-we2sc
@RitikRaj-we2sc 8 күн бұрын
When combined with react-hook-form and zod, client side validation doesn't work. Can you please tell why ?
@amitay1599
@amitay1599 10 күн бұрын
The official documentation doesn't provide examples of useActionState being used outside a . You're the first person I've seen using it this way-it does make sense conceptually, but I'm trying to understand if this approach goes against the intended paradigm.
@mahmoudfarghly2970
@mahmoudfarghly2970 6 ай бұрын
This hook is way more better than useFormState
@daytatech-youtube
@daytatech-youtube 6 ай бұрын
agreed dealing with pending status in an external component was kindof clumsy
@mahmoudfarghly2970
@mahmoudfarghly2970 6 ай бұрын
@@daytatech-youtube + using useFormState to retreive data from the server was also annoying
@sajarmohammed8045
@sajarmohammed8045 6 ай бұрын
How can i clear my input fields after submitting the form using server action?
@troquelina
@troquelina 6 ай бұрын
Do you have the repository for this new exciting thing you so clearly explained? Thanks!
@AlexanderBelov-y8o
@AlexanderBelov-y8o 3 ай бұрын
Can I return a success message from the action?
@xzex2609
@xzex2609 6 ай бұрын
I am confused 😕 , in documents useAcrionState returns two value , state and Action , but you have a pending too
@herozero777
@herozero777 4 ай бұрын
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?
@reactjs1900
@reactjs1900 6 ай бұрын
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 6 ай бұрын
Amazing!
@Monstermash355
@Monstermash355 6 ай бұрын
how to activate support for it ? i get an error message
@alexdin1565
@alexdin1565 5 ай бұрын
please can you make a video about Search and Pagination using server?
@CarlosIsaacRSison
@CarlosIsaacRSison 2 ай бұрын
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
@Michaeltje01
@Michaeltje01 6 ай бұрын
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.
@ismailtirmizi
@ismailtirmizi 4 ай бұрын
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. 6 ай бұрын
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) ?
@olusanyaolamide9764
@olusanyaolamide9764 6 ай бұрын
Thank you Brad
@MicroViewers-j1s
@MicroViewers-j1s 6 ай бұрын
it's very helpful
@tristandg
@tristandg 6 ай бұрын
just starting the video but isnt it the same as using action with useMutation ?
@nasko235679
@nasko235679 6 ай бұрын
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 6 ай бұрын
Yeah they’re more than just functions on the server. They essentially open a POST API endpoint on the server
@kingston7151
@kingston7151 6 ай бұрын
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
@excalibur2417
@excalibur2417 6 ай бұрын
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 6 ай бұрын
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 6 ай бұрын
@@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
@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).
@FaizKhan-jk9po
@FaizKhan-jk9po 5 ай бұрын
Can anybody tell me the name of vs code theme that ByteGrad currently using?
@wolfie8748
@wolfie8748 Ай бұрын
I also wonder
@DmGs-h1i
@DmGs-h1i 6 ай бұрын
it is work only in canary version nextjs
@yj4105
@yj4105 6 ай бұрын
what about useTransition?
@mrlectus
@mrlectus 6 ай бұрын
so why is previous state typed as any instead of number
@doppeltevier
@doppeltevier 6 ай бұрын
Could you create a video about Atlassian's new framework-agnostic, low-level dnd library called "pragmatic-drag-and-drop"?
@ianc6418
@ianc6418 6 ай бұрын
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 6 ай бұрын
Still RC, but should be stable soon
@AleRovella
@AleRovella 6 ай бұрын
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 6 ай бұрын
Yeah I’m using Next 15RC in the video, should be stable soon
@AleRovella
@AleRovella 6 ай бұрын
@@ByteGrad i want this now! jajaja, it's a better approach than useFormState
@abhishek427821
@abhishek427821 6 ай бұрын
Can we use this with redux?
@sawos1596
@sawos1596 6 ай бұрын
Using useActionState with useTransition is a good approach ? Or is it not always necessary ? Thank you in advance 🙏
@kurwacherry
@kurwacherry 6 ай бұрын
couldnt get it working on my nextjs project, do i need to update to a higher nextjs version? or use experimental?
@ByteGrad
@ByteGrad 6 ай бұрын
It will work in Next 15, I’m using Next 15 RC in the video
@kassiogluten
@kassiogluten 4 ай бұрын
canary version only ?
@EnesKab
@EnesKab 5 ай бұрын
PreviousState would be useful for "undo" operation right after user edited something.
@kashmirtechtv2948
@kashmirtechtv2948 6 ай бұрын
CREATE A VIDEO COVERING ALL ESSENTIAL CONCEPTS OF NEXT JS AND THOSE THAT WILL HELP US IN REAL PROJECTS
@wellbrutin
@wellbrutin 4 ай бұрын
thanksss
@phucoan9098
@phucoan9098 6 ай бұрын
i cant import useActionState from react like u, i dont know why
@ByteGrad
@ByteGrad 6 ай бұрын
Because this is in version 15 RC, it’s not stable yet
@kashmirtechtv2948
@kashmirtechtv2948 6 ай бұрын
great
@0xtz_
@0xtz_ 6 ай бұрын
thats cool 😎 but still sticking with trpc for production hhhh
@lukq90
@lukq90 4 ай бұрын
We returned to react query
@A.Floatrx
@A.Floatrx 6 ай бұрын
useTransition also helps display pending status during server action execution….
@indimerz
@indimerz 5 ай бұрын
i think this hook is still experimental. i cant import it by just entering its name.
@ByteGrad
@ByteGrad 5 ай бұрын
I'm using Next 15RC in the video, that's why it works for me
@indimerz
@indimerz 5 ай бұрын
@@ByteGradohk, can we upgrade project from next 14 to 15 with single command?
@maxrusakovic
@maxrusakovic 5 ай бұрын
@@ByteGrad Please, mention it in description or video name =)
@eliamzg
@eliamzg 6 ай бұрын
How dare you get sponsorship from an actual useful and related service and make the ad something useful to boot?
@mDHARYL
@mDHARYL 6 ай бұрын
Wow
@ibrvimv
@ibrvimv 6 ай бұрын
I just watched: "No - /API, Yes - Server Actions". Next video reccomended: "No - Server Actions, Yes - useActionState" 😅
@CadisDiEtrama000
@CadisDiEtrama000 6 ай бұрын
ReactQuery much? 😂😂
@mrlectus
@mrlectus 6 ай бұрын
Pagination, infinitQuery ...
@MrFinchBen
@MrFinchBen 6 ай бұрын
Let me put tutorial into tutorial, so you have tutorial in your tutorial :D
@protonys
@protonys 6 ай бұрын
it is work only in the canary version nextjs
Why I don't use React-Query and tRPC in Next.js
18:58
ByteGrad
Рет қаралды 100 М.
The evil clown plays a prank on the angel
00:39
超人夫妇
Рет қаралды 53 МЛН
Mom Hack for Cooking Solo with a Little One! 🍳👶
00:15
5-Minute Crafts HOUSE
Рет қаралды 23 МЛН
My scorpion was taken away from me 😢
00:55
TyphoonFast 5
Рет қаралды 2,7 МЛН
Learn useActionState In 8 Minutes - React Hooks Simplified
8:44
Web Dev Simplified
Рет қаралды 30 М.
The Problem with React Server Actions
9:45
Josh tried coding
Рет қаралды 23 М.
React 19 STABLE - I Can't Believe They Changed This
11:18
ByteGrad
Рет қаралды 35 М.
This React Hook is a Game Changer (useActionState)
15:03
Jan Marshal
Рет қаралды 2,9 М.
Forms with React 19 and Next.js
9:13
leerob
Рет қаралды 32 М.
Learn React Hooks: useTransition - Simply Explained!
10:41
Cosden Solutions
Рет қаралды 30 М.
Finally.... But it's exciting!
13:58
Maximilian Schwarzmüller
Рет қаралды 31 М.
Is Next.js 15 any good? "use cache" API first look
8:16
Beyond Fireship
Рет қаралды 129 М.
The evil clown plays a prank on the angel
00:39
超人夫妇
Рет қаралды 53 МЛН