React Actions & useActionState
Define server and client actions, handle form submissions, and track pending states.
React Actions & useActionState is a free React Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Welcome
What Are React Actions?
Server Actions in Next.js
'use server';
export async function createPost(formData) {
const title = formData.get('title');
await db.insert(posts).values({ title });
revalidatePath('/posts');
}Using an Action on a Form
import { createPost } from './actions';
export default function NewPostForm() {
return (
<form action={createPost}>
<input name="title" />
<button type="submit">Create</button>
</form>
);
}useActionState
'use client';
import { useActionState } from 'react';
import { createPost } from './actions';
function NewPostForm() {
const [state, action, isPending] = useActionState(createPost, null);
return (
<form action={action}>
{state?.error && <p>{state.error}</p>}
<input name="title" />
<button disabled={isPending}>Create</button>
</form>
);
}Action Returning State
'use server';
export async function createPost(prevState, formData) {
const title = formData.get('title')?.toString();
if (!title) return { error: 'Title is required' };
await db.insert(posts).values({ title });
return { success: true };
}isPending for Disabled Submit
<button type="submit" disabled={isPending}>
{isPending ? 'Saving...' : 'Save'}
</button>Client Actions
async function clientAction(prevState, formData) {
const res = await fetch('/api/post', {
method: 'POST', body: formData,
});
if (!res.ok) return { error: 'Failed' };
return { success: true };
}useFormStatus for Child Components
'use client';
import { useFormStatus } from 'react-dom';
function SubmitButton() {
const { pending } = useFormStatus();
return <button disabled={pending}>Submit</button>;
}Progressive Enhancement
Quick Check
Recap
Up Next
Frequently asked questions
Is the “React Actions & useActionState” lesson free?
Yes — the full text of “React Actions & useActionState” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.
What will I learn in “React Actions & useActionState”?
Define server and client actions, handle form submissions, and track pending states. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start React Academy?
No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “React Actions & useActionState” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this React Academy lesson?
Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- React Server Components Explained
- The use() Hook for Async Resources
- React Actions & useActionState
- useOptimistic for Instant Feedback