Next.js Discord

Discord Forum

Server Actions

Answered
DoyteDoyenr posted this in #help-forum
Open in Discord
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
View full answer

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)

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
Answer