Server Actions
Answered
DoyteDoyenr posted this in #help-forum
Original message was deleted.
Answered by Turkish Van
You can always call them manually as You would call any other function.
Checkout for more, here:
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#non-form-elements
Checkout for more, here:
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#non-form-elements
5 Replies
show your code @DoyteDoyenr also which version of nextjs are you on
@Arinji show your code <@846372267360387112> also which version of nextjs are you on
I made three attempts, this one which would be the action directly on the button as shown in the image of the topic (it didn't work)
This one using FormAction, but I didn't get any response (this was more of a test to see if anything would happen)
And finally, this one worked by using the button inside a form
export default function Home() {
async function publish(formData: FormData) {
"use server";
console.log('aaaa')
}
return <button action={publish}>Publish</button>;
}This one using FormAction, but I didn't get any response (this was more of a test to see if anything would happen)
export default function Home() {
async function publish(formData: FormData) {
"use server";
console.log('aaaa')
}
return <button formAction={publish}>Publish</button>;
}And finally, this one worked by using the button inside a form
type UserProps ={
id: string;
name: string;
}
export async function ListUser() {
const response = await fetch('http://localhost:8080/user',{method: 'GET', cache: 'no-store', next:{
tags:['get-user']
}})
const data:UserProps[] = await response.json()
const handleDelete = async (form: FormData) => {
'use server'
console.log('teste', form.get('id'))
}
return (
<section className="flex flex-col">
{data.map((item) =>(
<li key={item.id} className="text-white flex">
{item.name}
<form action={handleDelete}>
<button className="size-5 bg-zinc-400 rounded-lg" value={'1'} name="id" type="submit">X</button>
</form>
</li>
))}
</section>
)
}@Arinji show your code <@846372267360387112> also which version of nextjs are you on
In summary, I just wanted to know if it's possible to use actions outside of a form, without any specific use, just to know if it's really possible
@DoyteDoyenr In summary, I just wanted to know if it's possible to use actions outside of a form, without any specific use, just to know if it's really possible
Turkish Van
You can always call them manually as You would call any other function.
Checkout for more, here:
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#non-form-elements
Checkout for more, here:
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#non-form-elements
Answer