Next.js Discord

Discord Forum

Use "onClick" in server component

Answered
neon posted this in #help-forum
Open in Discord
Is there a way to call a function (that updates my database) when a button is clicked, but inside a server component? Maybe I can use some server actions magic?

9 Replies

@neon Is there a way to call a function (that updates my database) when a button is clicked, but inside a server component? Maybe I can use some server actions magic?
wrap your button in form.

return (
<form>
  <button handleAction={async () => {
  "use server"
  console.log("This is printed in the server")
}}>Do something</button>
</form>
)


do note: that client components are not a deoptimization. why would you want to create a onClick with server component?
you can use async function inside client components
really?
yep
just need to know how to
Can you teach please?
Answer
ty