Skip to content

Instantly share code, notes, and snippets.

@av1v3k
Created October 27, 2025 13:45
Show Gist options
  • Select an option

  • Save av1v3k/018b3b9faedfe66faed409d86c6c5800 to your computer and use it in GitHub Desktop.

Select an option

Save av1v3k/018b3b9faedfe66faed409d86c6c5800 to your computer and use it in GitHub Desktop.
useActionState() - starter
Product Form:
============
"use client"
import { useActionState } from "react";
import { productAction } from "@/app/actions/product";
export default function ProductForm() {
const initialState = {
message: null,
success: false
}
const [state, action, isPending] = useActionState(productAction, initialState);
return (
<form action={action}>
<input type="text" name="firstname"/>
<button type="submit" disabled={isPending}>Submit</button>
{state.message}
</form>
)
}
Product Action:
===============
"use server"
export async function productAction(previousState, formData: FormData) {
await new Promise((resolve) => setTimeout(resolve, 2000));
const data = {
firstname: formData.get('firstname') as string
}
console.log(data);
if(data.firstname == '') {
return {
message: 'Error, Firstname can\'t be empty',
success: false
}
}
return {
message: 'Success ! submitted the form',
success: true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment