# Server Actions (Non-Forms)

> Triggering server mutations without HTML forms.

Subject: Next.js | Lesson 32 of 65 | 6 min read
Interactive lesson: https://codewithanimation.in/nextjs/server-actions-non-forms
Previous: [Server Actions (Forms)](https://codewithanimation.in/nextjs/server-actions-forms.md) | Next: [Form State (useActionState)](https://codewithanimation.in/nextjs/use-action-state.md)

## Beyond Forms

While Server Actions are tightly integrated with `<form>`, they are fundamentally just async functions.

You can import a Server Action into any Client Component and call it directly like a normal JavaScript function. Next.js automatically transforms the call into a secure HTTP POST request behind the scenes.

## Micro-Mutations

Wrapping every single interactive element in a `<form>` can bloat your markup and feel restrictive.

For micro-mutations—like clicking a 'Like' button, dragging an item to reorder a list, or autosaving a draft—calling an action directly from an event handler like `onClick` or `onDrop` is much more ergonomic.

## Direct Invocation

Inside a Client Component, assign the action to an event handler: `<button onClick={async () => await upvotePost(id)}>Upvote</button>`.

You can pass standard JavaScript arguments to the action (like an ID or an object), rather than relying solely on `FormData` extraction.

## Common mistake: Losing Progressive Enhancement

The major trade-off is the loss of Progressive Enhancement. Because the action is bound to a JavaScript event (`onClick`), it strictly requires the browser to have downloaded and executed the JS bundle.

If JavaScript fails to load, clicking the button will do absolutely nothing, leaving the user confused.

## Tip: Handling Pending States

When you use a form, you can use `useFormStatus` to easily disable submit buttons while loading.

For non-form actions, `useFormStatus` will not work. You must use React's `useTransition` hook to manually track the pending state and update your UI accordingly.

## Try it: The Silent Failure

Test an 'Upvote' button that uses a non-form Server Action. With JS enabled, it works seamlessly. Turn JS off, and watch how the lack of a `<form>` wrapper results in a complete, silent failure.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/server-actions-non-forms

## Check yourself

### Question 1

What happens if a user clicks a button with an `onClick={myServerAction}` handler, but JavaScript is disabled?

- A. Next.js falls back to a native HTTP GET request
- B. The action executes successfully but the page reloads
- C. Nothing happens; the click is ignored
- D. A React Hydration error is thrown

Correct answer: C

Directly invoking an action from an event handler requires JavaScript. Without it, the browser has no instruction on what to do when the button is clicked.

- Why not A: Native fallbacks only work with the <form action={}> attribute.
- Why not B: Without a form wrapper, no HTTP request is made at all.
- Why not D: It fails silently; the event handler simply doesn't exist.

### Question 2

When calling a Server Action directly from a Client Component (not in a form), how do you pass data to it?

- A. You must manually construct a FormData object
- B. You pass standard JavaScript arguments (like strings, numbers, or objects)
- C. You pass them as URL Search Parameters
- D. You can only pass strings

Correct answer: B

Unlike form actions which receive `FormData`, directly invoked Server Actions act like Remote Procedure Calls (RPCs) and accept standard JavaScript arguments.

- Why not A: You can pass normal arguments directly.
- Why not C: Next.js handles the networking automatically; you just pass function arguments.
- Why not D: You can pass complex serializable objects.

## Remember this

- Server Actions can be called directly from Client Components like normal functions.
- This is ideal for UI interactions that aren't form submissions (e.g., Like buttons).
- Direct invocation allows you to pass standard arguments instead of `FormData`.
- WARNING: Non-form actions completely lose Progressive Enhancement.
- If JavaScript is disabled, the action will not fire.
