Snap
Snap

Reputation: 739

NextJS: Using Form to create button to trigger server action

I have a NextJS project using the app router and I need a button that deletes a cookie when clicked. I'm achieving this by creating a form that contains only the button so that way the sever action can be triggered via the button:

"use server";
import { cookies } from "next/headers";

const deleteCookie = async () =>
{
    cookies().delete("my-cookie");
};

export default async function Component()
{

    return (
        <form action={deleteCookie}>
            <button type="submit">Delete Cookie</button>
        </form>
    );
}

Is this the correct way to achieve this? It does work. It just seems like kinda of an odd way of doing things.

Upvotes: 1

Views: 354

Answers (0)

Related Questions