Next.js best practices
Answered
American black bear posted this in #help-forum
American black bearOP
Hi guys. I have a sidebar component where I added a logout link that points to
I want to avoid creating unnecessary client components. Is this a good practice or is there a better approach?
/api/logout
. However, my logout api route only clears the cookies and redirects the user to the /login
page.I want to avoid creating unnecessary client components. Is this a good practice or is there a better approach?
Answered by luis_llanes
Forms work better when they’re Client Components, you get all the benefits when you manage them from the client: error states, loading states, real time validations, etc.
15 Replies
It's fine. After all you need to delete cookies in either a Server Action or Route Handler and call it from the Client.
This is definitely a good practice, why wouldn't it be?.
Curious to know why do you think this might not be a good practice. In fact, Client Components are by themselves a good practice remove that from your mental model if you think you're doing things wrong by using Client Components
This is definitely a good practice, why wouldn't it be?.
Curious to know why do you think this might not be a good practice. In fact, Client Components are by themselves a good practice remove that from your mental model if you think you're doing things wrong by using Client Components
@luis_llanes It's fine. After all you need to *delete* cookies in either a Server Action or Route Handler and **call it from the Client**.
This is definitely a good practice, why wouldn't it be?.
Curious to know why do you think this might not be a good practice. In fact, **Client Components are by themselves a good practice** remove that from your mental model if you think you're doing things wrong by using Client Components
American black bearOP
I'm sorry, I think I din't understand
in your opinion it's fine to keep the api route and call it inside a client component?
Yes, it’s fine.
You could also make that a Server Action, both will work the same (server actions are a POST endpoint under the hood anyway)
You could also make that a Server Action, both will work the same (server actions are a POST endpoint under the hood anyway)
Maybe the easiest way is to move that logic to a server action so you can simply call the function from your client component, but if you stick with Route Handlers that also works
Server Actions reduce the boilerplate you need to write to achieve the same result
American black bearOP
ye that makes sense
what if I create a server action and add a form inside the server component?
or a client component it's better for this case?
Forms work better when they’re Client Components, you get all the benefits when you manage them from the client: error states, loading states, real time validations, etc.
Answer
Answering your question, you could call the Server Action from the Server component <form>, but that only covers a small range of possibilities
American black bearOP
kk
thanks a lot
🙂
Of course!
Let me know if you need more help
Let me know if you need more help