Mutate data using Server Actions in NextJs 13.4

  Рет қаралды 11,549

Hamed Bahram

Hamed Bahram

Күн бұрын

Пікірлер: 90
@kyle-andrewgovinder9902
@kyle-andrewgovinder9902 Жыл бұрын
What I love the most about Hamed's videos is that he covers literally every detail no matter how small it seems. He apologized for self-promotion but there was no need to apologize because it just shows how much of content this man has already created for us on Nextjs and was just pointing us in the right direction in case we needed more info on the topics he was discussing.
@hamedbahram
@hamedbahram Жыл бұрын
Thanks Kyle! I appreciate that.
@Shawn-Mosher
@Shawn-Mosher Жыл бұрын
Very cool!!! Gets better and better. Good job showing us these new updates
@hamedbahram
@hamedbahram Жыл бұрын
Anytime Shawn!
@rebazjabar7660
@rebazjabar7660 Жыл бұрын
so excite to see you're working with up-to-date topic. we appreciate you
@hamedbahram
@hamedbahram Жыл бұрын
Thanks Rebaz. Glad you found it helpful.
@BuiltWithLLMs
@BuiltWithLLMs Жыл бұрын
Only person doing MongoDB + Next 13 right now, thanks!
@BuiltWithLLMs
@BuiltWithLLMs Жыл бұрын
State management in next 13 would also be interesting
@hamedbahram
@hamedbahram Жыл бұрын
That's right David, you won't see mongo that often these days.
@hamedbahram
@hamedbahram Жыл бұрын
Agreed. That's an interesting topic to cover next. Thanks for the feedback.
@arman4887
@arman4887 Жыл бұрын
I feel blessed to have found this channel. Great video on up-to-date Nextjs topics with MongoDB
@hamedbahram
@hamedbahram Жыл бұрын
Happy to have you here Arman. I'm glad you found the content helpful.
@Billiam112
@Billiam112 Жыл бұрын
Great stuff, looking forward watching the whole thing :)
@hamedbahram
@hamedbahram Жыл бұрын
Thanks for tuning in Bill!
@abibinyun
@abibinyun Жыл бұрын
thx for detail information, absolutely amazing, i like how you description detail feature from next js,
@hamedbahram
@hamedbahram Жыл бұрын
Your welcome! Thanks for tuning in :)
@noahginsburg6140
@noahginsburg6140 Жыл бұрын
Awesome video. Love that you’re taking the time to keep up with the current fire hose of nextjs updates. Here’s my question, in your lib folder where you define the mongodb methods couldn’t you have a method that makes the db connection and makes your db mutation similar to how you directly go to mongo to GET the todos? What are the direct benefits of the actions? Also, would you typically turn those GET methods into an api? I’ve always just made apis for data access without considering just making a method and going right to the db. Thanks for the great content!
@hamedbahram
@hamedbahram Жыл бұрын
Thanks Noah for leaving your feedback. Making APIs to access your database was the way. But with the new server actions, you don't really need that extra layer, you can define functions that run on the server and just call them from the client.
@LifeWithRilla
@LifeWithRilla Жыл бұрын
@@hamedbahram So if I'm understanding correctly the server actions just remove the API layer?
@hamedbahram
@hamedbahram Жыл бұрын
@@LifeWithRilla I think it's fair to say that.
@Billiam112
@Billiam112 Жыл бұрын
If I may suggest for a future video; I would be super interested in a nextjs + strapi video.👌
@hamedbahram
@hamedbahram Жыл бұрын
You got it!
@mobpsycho6600
@mobpsycho6600 Жыл бұрын
It was worth to watch this video
@hamedbahram
@hamedbahram Жыл бұрын
Thanks for tuning in!
@eloualielouali4949
@eloualielouali4949 Жыл бұрын
top notch video, i was wandering if u want to add a delete button next to every todo how would u handel the action
@hamedbahram
@hamedbahram Жыл бұрын
That'll be the same, you can have a button with 'fromAction' prop on it point to a 'deleteTodo' action, or a form the same way with an 'action' prop
@eloualielouali4949
@eloualielouali4949 Жыл бұрын
@@hamedbahram thank u so much for ur time
@hamedbahram
@hamedbahram Жыл бұрын
@@eloualielouali4949 my pleasure
@lulungsatrioprayuda9931
@lulungsatrioprayuda9931 Жыл бұрын
I'm so excited to follow you tutorial sir, did you have any tutorial that create simple auth with jwt token? thank you
@hamedbahram
@hamedbahram Жыл бұрын
Thanks Lulung. I use `next-auth` for authentication. It's pretty simple and straight forward. You can watch it here: kzbin.info/www/bejne/e5mWdqOtprlnma8
@hfzkq
@hfzkq Жыл бұрын
Awesome ❤Go ahead
@hamedbahram
@hamedbahram Жыл бұрын
Thanks! Let's go!
@alirezadnia
@alirezadnia Жыл бұрын
Perfect. Thank you Hamed
@hamedbahram
@hamedbahram Жыл бұрын
My pleasure buddy! Thanks for tuning in.
@ofermalbin
@ofermalbin Жыл бұрын
Thanks for the video! Could you give an example of a field of type file? How do I extract the name of the file and its contents in action?
@hamedbahram
@hamedbahram Жыл бұрын
Good question Ofer. I'll definitely cover that in a future video. Thanks for tuning in.
@juanferrer5885
@juanferrer5885 Жыл бұрын
Awesome video!! Could you talk about internationalization in nextjs AppRouter?
@hamedbahram
@hamedbahram Жыл бұрын
Thanks Juan. Sure, that'll be an interesting topic. Will keep that in mind.
@GregPeters1
@GregPeters1 Жыл бұрын
Thanks for the heads up!
@hamedbahram
@hamedbahram Жыл бұрын
Anytime! Thanks for tuning in Greg.
@AnthonySmith-tw2ll
@AnthonySmith-tw2ll Жыл бұрын
Great content, thank you! I can't seem to get server actions to work on pages generated with generateStaticParams... any idea why? I'm getting 405 method not allowed but the same component submits data fine on other pages.
@hamedbahram
@hamedbahram Жыл бұрын
You're welcome Anthony, thanks for tuning in. I'll test that out. In the mean time, can you do the following and let me know, I'm assuming this is a server-rendered page, can you comment the `generateStaticParams` function out and then try on that same page? Also, do you have a `route.js` file defined at that segment?
@AnthonySmith-tw2ll
@AnthonySmith-tw2ll Жыл бұрын
@@hamedbahram Thanks for the quick reply, Hamed. If I comment out the function then it does work - so it does appear to be a conflict there but not sure how to fix currently. The structure is reviews(folder) -> [slug](folder) -> page.tsx (file containing generateStaticParams)
@AnthonySmith-tw2ll
@AnthonySmith-tw2ll Жыл бұрын
@@hamedbahram I've used notfound from next/navigation instead of fetching the static paths up front. I think that fetching the static paths means that you can't use a server action on that page. I get the same result and am able to use server actions now so I think this is the method to use going forward.
@hamedbahram
@hamedbahram Жыл бұрын
I see. I'd have to test this out myself.
@oluwafemifaleye6845
@oluwafemifaleye6845 Жыл бұрын
Thank you for this interesting video. Please, how can I handle file upload using the server component?
@hamedbahram
@hamedbahram Жыл бұрын
Thanks Oluwafemi! I'll be covering that in a future video soon.
@rajenderkatkuri7642
@rajenderkatkuri7642 Жыл бұрын
What font do you use in VS Code?
@hamedbahram
@hamedbahram Жыл бұрын
Operator mono!
@alvinacosta2312
@alvinacosta2312 Жыл бұрын
how to redirect to a different page after a todo is created sir? previously we had to use useRouter. how to do it in a server component after the server actions is done. thanks!
@hamedbahram
@hamedbahram Жыл бұрын
You can call `redirect` from `next/navigation` in your server action function.
@ARUNRAWAT-rh5ks
@ARUNRAWAT-rh5ks Жыл бұрын
could a make a video on how cna we update the session info after user logged in ?
@hamedbahram
@hamedbahram Жыл бұрын
Thanks for the feedback. I'll have that in mind for a future video. In the mean time you can read more about the new `update` function from the next-auth package: next-auth.js.org/getting-started/client#updating-the-session
@ИванАрабаджийски
@ИванАрабаджийски Жыл бұрын
How are you supposed to handle loading state and errors using this server actions? What if I double click the button while the first call is still being awaited?
@hamedbahram
@hamedbahram Жыл бұрын
Good question! you can use the `useTransition` hook or the `experimental_useFormStatus` from react to get a pending state while executing the server action. For error handling you can watch this video where I use zod to validate the inputs => kzbin.info/www/bejne/qZWud3ila55qbrc
@soundnimation
@soundnimation Жыл бұрын
Cool video)) I am looking for a method to save the state of the page after reloading, can you recommend something, please? I want to avoid storage or cookies, what I am looking for is something that was in a react-router page state. Thank you in advance. For example, menu is open and after reloading it is closed, or the pagination state or filter state
@hamedbahram
@hamedbahram Жыл бұрын
Different ways you can do it, for example you can use the URL for some state, things like filter and pagination can be persisted in the URL, for UI state you can use the React context.
@DioArsya
@DioArsya Жыл бұрын
is using actions function like that doesn’t expose the important credentials on the server side?
@hamedbahram
@hamedbahram Жыл бұрын
That's a good question! server actions are only running on the server, but it's important to understand how to use them correctly. Also keep in mind that this is still in alpha release and not production ready in any shape or form, so we'll most likely see changes in the future.
@eliuddyn
@eliuddyn Жыл бұрын
Great 🔥🔥
@hamedbahram
@hamedbahram Жыл бұрын
Thanks for tuning in.
@AbdallahMoubarak
@AbdallahMoubarak Жыл бұрын
Hello Hamed, How can we add mongoose to this and use the models?
@hamedbahram
@hamedbahram Жыл бұрын
Hello Abdallah, it would be very similar to what I'm doing in this video, you can define your models and expose your database access objects (functions) like I did in the `lib/mongo/todos.js` file. However, I don't recommend using mongoose, if you're looking to use an ORM, use Prisma, which gives you type-safety as well. I have a video coming out tomorrow, where I demonstrate this.
@AbdallahMoubarak
@AbdallahMoubarak Жыл бұрын
@@hamedbahram thank you bro..
@hamedbahram
@hamedbahram Жыл бұрын
@@AbdallahMoubarak My pleasure.
@damiantriebl1747
@damiantriebl1747 Жыл бұрын
and for use Redux with client and server component (in the same store) is posible?
@hamedbahram
@hamedbahram Жыл бұрын
I'm not sure how you would fit Redux into this architecture. I would use native Javascript patterns like modules to share data and logic between server components and React context for client components.
@KendaBeatMaker
@KendaBeatMaker Жыл бұрын
@@hamedbahram hey guys did a little experiment and was able to get recoil working by creating a component that takes children and wrap them with recoilroot. Then wrap the root layout with this created component (recoilroot)
@hamedbahram
@hamedbahram Жыл бұрын
@Ken Taylor Cool! Share your solution if you don't mind.
@Fantasia1013
@Fantasia1013 Жыл бұрын
what means "mutate data" ?
@hamedbahram
@hamedbahram Жыл бұрын
It means Changing or updating your data
@Fantasia1013
@Fantasia1013 Жыл бұрын
@@hamedbahram ty so much
@hamedbahram
@hamedbahram Жыл бұрын
@@Fantasia1013 Anytime!
@techcoevolve
@techcoevolve Жыл бұрын
Cool shirt buddy :)
@hamedbahram
@hamedbahram Жыл бұрын
Thanks 🙏🏽
@moinkhanpathan541
@moinkhanpathan541 Жыл бұрын
can we update data using server action?
@hamedbahram
@hamedbahram Жыл бұрын
Absolutely! server mutation is exactly updating data using server actions.
@0xtz_
@0xtz_ Жыл бұрын
Woooow
@hamedbahram
@hamedbahram Жыл бұрын
My pleasure 😀
@leopolddelassence5224
@leopolddelassence5224 Жыл бұрын
very cool, but video is slightly too long
@hamedbahram
@hamedbahram Жыл бұрын
Thanks for your feedback. I'll try to make future videos more concise.
@misterkilluminati6949
@misterkilluminati6949 Жыл бұрын
Thanks for the amazing and straight forward contents :) Quick questions though. Do we have to make a route segment, which has a form that has a server action on it, dynamic(AKA SSR) ?
@hamedbahram
@hamedbahram Жыл бұрын
Thanks! glad you find the content useful. Good question! no you don't have to make it dynamic, in fact unless you're using a dynamic function like `headers`, `cookies`, etc. it will be static by default.
@misterkilluminati6949
@misterkilluminati6949 Жыл бұрын
@@hamedbahram I was actually remaking the project you did on your video about how SSR, SSG and ISR are different from each other, and changing your Form component to use server actions instead of onSubmit. The problem is, if I use a server action on the form component, all the three SSG, SSR and ISR pages which use that Form component, will turn SSR when I build the project. I was hoping you could give a hint of what's going on. There's nothing on the docs about this
@hamedbahram
@hamedbahram Жыл бұрын
Hmm 🤔, Interesting! cool idea to combine server actions with the whole dynamic vs static rendering and see the result. I'd have to test that out
@misterkilluminati6949
@misterkilluminati6949 Жыл бұрын
@@hamedbahram I would love to see a video on it from you in the future :) Thankss
@hamedbahram
@hamedbahram Жыл бұрын
@@misterkilluminati6949 Absolutely
Using TypeScript in NextJs 13
17:42
Hamed Bahram
Рет қаралды 6 М.
Server vs client components in NextJs 13 - When to use which
34:07
Hamed Bahram
Рет қаралды 35 М.
Thank you mommy 😊💝 #shorts
0:24
5-Minute Crafts HOUSE
Рет қаралды 33 МЛН
tRPC + NextJS App Router = Simple Typesafe APIs
19:18
Jack Herrington
Рет қаралды 85 М.
Self-Hosting Next.js
45:13
leerob
Рет қаралды 78 М.
Why I use Zod with Server Actions
15:21
Ryan Toronto
Рет қаралды 13 М.
Learn Next.js 13.4 Server Actions in 24 minutes (For beginners)
24:45
Next's Server Actions Might Not Be That Safe...
6:43
Web Dev Cody
Рет қаралды 13 М.
Server Actions: NextJS 13.4's Best New Feature
21:41
Jack Herrington
Рет қаралды 61 М.