Updating user info in db using forms
I have a basic user model
I have a form that allows the user to update email, username, etc.
I'm using a simple update method
For those using nextjs, is it best to put this function in a server action or make an api route and call it there?
5 Replies
Also is best to update by
id
instead of username
?You could go with either of the two options. API routes would be more preferable as it can be called from anywhere in your application, whether it’s from a component, a different page, or even an external service. So, if you are looking for reusability then go with API routes.
Server Actions makes more sense if the data is required during page rendering, then it can be more straightforward to handle it in the server action.
I would recommend updating via
id
.@Nurul (Prisma) Here's what I have setup for using server actions. In
page.tsx
for profile
I need to pass updateUser
as a prop to <ProfileForm updateUser={updateUser}/>
right?
Then inside my <ProfileForm/>
client component I have the submit function of the form as the following
I believe you can do that 👍
I would recommend trying it out first and ensure everything is working as expected.
server action
this work correctly for and username is updated in the db