Async Forms — Complete Guide
Async Forms — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of React.js Tutorial on Toolliyo Academy.
On this page
React.js Tutorial · Lesson 42 of 100
Async Forms
Beginner ✓ → Intermediate → Advanced → Professional
Intermediate · 2 — Build apps · ~14 min read · Module 5: State & Authentication
Introduction
You know the basics now. Here we use Async Forms in real app situations — forms, pages, and data. Still plain language, just a bit more depth. Async forms submit data to an API and wait for the response. You show loading on the button, handle success and error, and maybe disable double-submit. Real sign-up, checkout, and profile forms call backends. Users need feedback while the network request runs.
State mistakes cause the most React bugs for beginners. Keep server data and UI state separate — this lesson shows how teams do that.
When will you use this?
Use these patterns when data is shared across pages or comes from a server.
- Shopping carts, user sessions, and API data need clear state patterns.
- Teams pick Context, Zustand, or Redux based on app size — you will see all three in jobs.
Real-world: Naukri signup with loading state
Job seeker signup disables button and shows spinner while /api/register runs — prevents double submit and duplicate accounts.
Production-style code
function SignupForm() {
const [pending, setPending] = useState(false);
const [error, setError] = useState(null);
async function handleSubmit(e) {
e.preventDefault();
setPending(true);
setError(null);
const data = new FormData(e.target);
try {
const res = await fetch('/api/register', { method: 'POST', body: data });
if (!res.ok) throw new Error('Signup failed');
window.location.href = '/dashboard';
} catch (err) {
setError(err.message);
} finally {
setPending(false);
}
}
return (
<form onSubmit={handleSubmit}>
{error && <p role="alert">{error}</p>}
<button disabled={pending}>{pending ? 'Creating account…' : 'Sign up'}</button>
</form>
);
}
What happens in production: Professional forms always communicate in-flight state — users do not hammer submit during slow mobile networks.
Lesson example (start here)
Copy this smaller example first. Once it works, compare it with the real-world code above.
async function onSubmit(data) {
setLoading(true);
setError('');
try {
const res = await fetch('/api/signup', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
if (!res.ok) throw new Error('Signup failed');
navigate('/welcome');
} catch (e) {
setError(e.message);
} finally {
setLoading(false);
}
}
Line-by-line walkthrough
| Code | What it means |
|---|---|
async function onSubmit(data) { | Defines a function — often a component or event handler. |
setLoading(true); | Part of the Async Forms example — read it together with the lines before and after. |
setError(''); | Part of the Async Forms example — read it together with the lines before and after. |
try { | Part of the Async Forms example — read it together with the lines before and after. |
const res = await fetch('/api/signup', { | Calls an API to load or save data from a server. |
method: 'POST', | Part of the Async Forms example — read it together with the lines before and after. |
headers: { 'Content-Type': 'application/json' }, | Part of the Async Forms example — read it together with the lines before and after. |
body: JSON.stringify(data) | Part of the Async Forms example — read it together with the lines before and after. |
}); | Closes a block started by { or ( above. |
if (!res.ok) throw new Error('Signup failed'); | Part of the Async Forms example — read it together with the lines before and after. |
navigate('/welcome'); | Part of the Async Forms example — read it together with the lines before and after. |
} catch (e) { | Closes a block started by { or ( above. |
setError(e.message); | Part of the Async Forms example — read it together with the lines before and after. |
} finally { | Closes a block started by { or ( above. |
How it works (big picture)
- setLoading disables the button.
- try/catch shows errors.
- finally always clears loading even if request fails.
Do this on your computer
- Add loading and error state.
- Make submit handler async.
- Disable button when loading is true.
- Read the real-world section and name which part of the app uses this topic.
- Run the example locally and confirm the same behavior in the browser.
- Change one value in the example (text, initial state, or URL) and predict what will happen before you save.
Experiments — try changing this
- Change text or labels in the example and save — watch the browser update.
- Break the code on purpose (remove a bracket), read the error message, then fix it.
- Open React DevTools (browser extension) while running Async Forms and inspect component props/state.
Remember
Async submit = loading + try/catch + user feedback. Prevent double submit while waiting. Show server error messages clearly.
Common questions
useEffect vs form submit for POST?
POST belongs in submit handler, not useEffect.
How long should I spend on Async Forms?
Until you can explain it in your own words and run the example without looking at the answer. Beginners often need 30–60 minutes per new hook or routing topic; setup lessons may take one afternoon.
What if I get stuck on Async Forms?
Re-read the line-by-line walkthrough, check the browser console for red errors, and compare your code character-by-character with the example. Search the exact error text — someone else had it too.
Where is Async Forms used in real jobs?
See the real-world section above — the same pattern appears in LMS, banking, e-commerce, and SaaS products. Interviewers ask you to explain it using one concrete example from your project or this lesson.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!