Skip to main content

The full picture

Creating a record involves three parts working together: a FastAPI endpoint, an API client function, and a React form component.

The form component

Key details:
  • onUserCreated callback — the form doesn’t manage the user list, it just notifies the parent
  • submitting state — disables the button to prevent double submissions
  • Form reset — clears inputs after successful creation
  • Error display — shows the error message from the API

The parent component

The parent owns the user list and passes the callback:
When the form creates a user, the API returns the full user object (with its new id). The parent adds it to the list. The UI updates instantly — no need to refetch.

Adding validation

Combine frontend validation with backend error handling:
Two layers of validation:
  1. Frontend — catches missing fields before sending the request (instant)
  2. Backend — catches things like duplicate emails (requires API call)
Clear field errors when the user starts typing in that field. This gives immediate feedback that they’re fixing the issue. The handleChange function checks if (errors[name]) to do this.

The data flow

Every step has error handling. The user always knows what happened.

What’s next?

You can create records. Now let’s display them — fetching a list from the API and rendering it in React.

Read operation

Fetch and display data from your FastAPI backend in React components