I Didn't Know Next.js Server Actions Could Do This!

  Рет қаралды 3,826

James Q Quick

James Q Quick

2 ай бұрын

There's more to Server Actions than I thought. I didn't know you could use a Server Actin outside of a form!
*Newsletter*
Newsletter 🗞 - www.jamesqquick.com/newsletter
*DISCORD*
Join the Learn Build Teach Discord Server 💬 - / discord
Follow me on Twitter 🐦 - / jamesqquick
Check out the Podcast - compressed.fm/
Courses - jamesqquick.com/courses
*QUESTIONS ABOUT MY SETUP*
Check out my Uses page for my VS Code setup, what recording equipment I use, etc. www.jamesqquick.com/uses

Пікірлер: 36
@dgcp354
@dgcp354 2 ай бұрын
i actually never used actions inside form action, i call the action inside on submit, etc
@kazmi401
@kazmi401 2 ай бұрын
I also do the same.
@victorhenriquecollasanta4740
@victorhenriquecollasanta4740 2 ай бұрын
me too
@lyrical6852
@lyrical6852 2 ай бұрын
How to do that? you mean like this? const onSubmit = handleSubmit(formData=> { serverActionFunction(formData); }); ...
@vichetnoeng706
@vichetnoeng706 2 ай бұрын
I actually use Server-Action with React-Query straight, no need to create additional API Route. It's amazing
@yassinebenazouz4529
@yassinebenazouz4529 2 ай бұрын
i accidentally tried this when the server actions first released and i was impressed. i never used the api folder after that accident x)
@karan4345
@karan4345 2 ай бұрын
This is so true. I accidentally called a function with 'use server' on button click. And I spent next hour finding why my API call is not visible in my network tab. Then eventually landed on next js docs and figured server action has more use cases than it was marketed for.
@JamesQQuick
@JamesQQuick 2 ай бұрын
bahaha that's funny but a valuable lesson I guess
@kenscode
@kenscode 2 ай бұрын
Really cool, can't wait to try it out 😎💪
@VincentFulco
@VincentFulco 2 ай бұрын
Great find
@gravellife5643
@gravellife5643 Ай бұрын
I'm used server actions in my project for Slot Machines. Every button(Spin, increase/decrease bet) calling server actions and calculate game on server, update game data in redis database and return win amount and new symbol indexes on slot reels.
@NickWelsh4444
@NickWelsh4444 2 ай бұрын
Server actions are amazing! I've been using them with next-safe-action for end-to-end type safety.
@JamesQQuick
@JamesQQuick 2 ай бұрын
Nice! Love that!
@xc1427
@xc1427 Ай бұрын
I think vercel ai sdk leverages this feature. And you can return react component from the server action.
@christophkovacs
@christophkovacs 2 ай бұрын
Yep we also changed our API route handlers to "inline" server actions and got rid of a lot of code. Was also wondering why this paragraph is not highlighted.
@nicolasguillenc
@nicolasguillenc 2 ай бұрын
You can also use it to validate the fields before submitting the form!
@kassios
@kassios 2 ай бұрын
That is a great use case that could be a video idea
@RavenXplod
@RavenXplod 2 ай бұрын
I always assumed that server actions worked this way, so i never used form data becuz i dont like it, i just use server actions and send inputs values from use state into server actions like a parameters. But one problem is that server actions can only return serialisable data, eg JSON but not blobs. In the next documentation it says that they support returning arraybuffer but thats not the case. So for just this use case i had to create an api endpoint to recieve a blob/binary data But yeah server actions are super awesome and i use them everywhere for every server code and its not even button, you can call server action in useEffect also.
@treyrader
@treyrader 2 ай бұрын
yea i noticed it when i first read the docs, but i didn't think it was a common practice so I just mix actions with api endpoints to get shit done. I just try sticking to what's common I guess. still learning the ropes lol. Guess you are too?
@heysahilsingh
@heysahilsingh 2 ай бұрын
The whole KZbin is filled with using server actions in form, come on, that’s not the only case. Thanks to you for making this video.
@JamesQQuick
@JamesQQuick 2 ай бұрын
Thanks! Yeah it's funny how not obvious it was that there are many more use cases!
@nasko235679
@nasko235679 6 күн бұрын
While fantastic on paper, calling an action from a button comes with it's own set of issues - mostly the issue of the client manipulating function parameters in a malicious way. Let's say I have a chat app and have an "add friend" button that executes an action which adds a friend request in my db and it takes 2 parameters - the user's username and the person they want to befriend's user name. How do I protect from the user changing their username and basically making a friend request on someone else's behalf? The answer is - you don't really. Only way is to fetch the user's username from an auth session and query the db and that can put an exponential load on your database especially if you're dealing with a lot of buttons in a dashboard/ friend's list type of page.
@alejandrobalderas2370
@alejandrobalderas2370 2 ай бұрын
One very cool thing is combining tanstack query with server actions 🚀 … you can call a server action as a mutation function and forget about exposing api routes that do not necessarily need to be public
@coder_one
@coder_one 2 ай бұрын
Next has aggressive caching mechanism. Using tools like tanstack has no reason
@leonarddugan5451
@leonarddugan5451 2 ай бұрын
Definitely not hidden. Since they became stable enough to actually use I haven’t written an api route once.
@jsantos1220
@jsantos1220 2 ай бұрын
HA!! i figure that the last week! hehe finally i did something before watching a tutorial
@JamesQQuick
@JamesQQuick 2 ай бұрын
Nice! Seems like we are in a similar position!
@vivekkaushik9508
@vivekkaushik9508 2 ай бұрын
My problem with this approach 'using server actions with event handlers' is that you've to mark the component as 'use client' which kinda defeats the whole point of using SSR and server actions. I've been struggling with this lately on how to fully leverage the SSR/RSC without invoking use client to handle events.
@kassios
@kassios 2 ай бұрын
How would you handle events on a server side component? ‘Use client’ is obvious for user interactivity. Interactivity is the most clear separation between server and client components
@heysahilsingh
@heysahilsingh 2 ай бұрын
@@kassios You’re right;
@alexnelson2119
@alexnelson2119 2 ай бұрын
Bruh. How do people not know this? No wonder people still complain about app router. Server actions and RSC are the new meta.
@JamesQQuick
@JamesQQuick 2 ай бұрын
Well for me, the example i always see is within a form. It just wasn't obvious you could do this in other places
@alexnelson2119
@alexnelson2119 2 ай бұрын
@@JamesQQuick An opportunity for Vercel to make this clearer in the docs for sure. Nice of you to point it out! Like others have mentioned, I barely use api routes anymore. Pro-tip, bust out useTransition and wrap your server action call in a startTransition for isPending state.
@liu-river
@liu-river Ай бұрын
How you only know this now, lol? You can throw it in a transition and get loading state too.
Why you can't set cookies in Server Components
10:58
Ryan Toronto
Рет қаралды 6 М.
Node.js Tutorial - 31 - Creating a Node Server
7:26
Codevolution
Рет қаралды 38 М.
Miracle Doctor Saves Blind Girl ❤️
00:59
Alan Chikin Chow
Рет қаралды 55 МЛН
Black Magic 🪄 by Petkit Pura Max #cat #cats
00:38
Sonyakisa8 TT
Рет қаралды 34 МЛН
КАХА и Джин 2
00:36
К-Media
Рет қаралды 4,2 МЛН
AI Hardware w/ Jim Keller
33:29
Tenstorrent
Рет қаралды 26 М.
Reddit Pixel Setup on GoHighLevel in 2024
3:38
Masum Jia
Рет қаралды
Google's New Core Web Vital (INP) Explained in 5 Minutes
7:06
James Q Quick
Рет қаралды 3,3 М.
Fetching Data Doesn't Get Better Than This
6:58
Josh tried coding
Рет қаралды 61 М.
The 3 REAL benefits of Next.js Server Actions
12:11
ByteGrad
Рет қаралды 23 М.
My Problem with Next.js Server Actions
10:13
James Q Quick
Рет қаралды 14 М.
What Is Recursion - In Depth
13:25
Web Dev Simplified
Рет қаралды 150 М.
What are my thoughts on next.js app router vs pages router
11:04
Web Dev Cody
Рет қаралды 21 М.
Miracle Doctor Saves Blind Girl ❤️
00:59
Alan Chikin Chow
Рет қаралды 55 МЛН