To create a form in React v18+ and NextJS App Router, there is some prerequisite knowledge required.
Prerequisites
This article assumes that you already have the following knowledge:
React
- Specifically what components and state are.
- React Server Components
Next
- How Server Side Rendering works
- How to use React Server Components in NextJS
- How to use Client Components in NextJS
- How to interleave Server and Client Components
Shameless plug: Feel free to check my article that covers all of these NextJS prerequisites: React Server Components VS Client Components in NextJS App Router | by janac | Jan, 2024 | Medium
High-level Summary
- Create form elements such as text inputs, checkboxes etc., in a React Client Component
- Use the
formAction
attribute on a<form>
to call a callback function that will receive the form data - Put the callback function into it’s own module (file) with the annotation
"use server";
— this makes the function a NextJS Server Action