BeginnerCoder

Lesson 16 — Form submit

Handle a form submit

When a form is submitted, the browser normally reloads the page. In React you stop that with e.preventDefault() and handle the data yourself. Keep the input text in state (a controlled input), and on submit copy it into another state to show or send it.

<form onSubmit={(e) => { e.preventDefault(); setQuery(search); }}>

Note: Next.js pages are React components, so this preview runs your code with plain React in the browser. Next.js-only features (the file router, server rendering, data fetching on the server) are not running here — where a lesson needs them, it shows a small simulation instead.

Why it matters: sign-up, login, search and contact forms all start exactly like this.

Want it another way?
Task

In onSubmit, stop the page reload and save the typed name into sent. Typing Sam and pressing Go should show "Hello, Sam!".

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Next.js Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.