Skip to main content
✅ Validators & Form Logic

What you're building

A pure-function formReducer that manages the state of a multi-field form — the same pattern React Hook Form, Redux Form, and custom useReducer hooks use under the hood.

Requirements

Implement formReducer(state, action) that handles these action types:

Action typePayloadEffect
SET_FIELD{ field, value }Update state.values[field]
SET_ERROR{ field, error }Set state.errors[field]
CLEAR_ERROR{ field }Delete state.errors[field]
SUBMIT—Set state.submitting = true, state.submitted = false
SUBMIT_SUCCESS—Set state.submitting = false, state.submitted = true
RESET—Return a fresh initial state

Initial state shape:

{ values: {}, errors: {}, submitting: false, submitted: false }

Also export initialState as a named const with that shape.

Pure functions only — never mutate the incoming state object. Return a new object each time.

Stretch goals

  • Add a SUBMIT_FAILURE action that sets submitting: false and records a top-level submitError string.
  • Add a SET_FIELDS bulk action that merges multiple fields at once.
  • Write a selectHasErrors selector that returns true when errors has any keys.

What this proves

Reducer pattern, immutability, and action-based state management — the same mental model used in Redux, Zustand, and React's useReducer. Every senior frontend role expects this.

⚡ 50 XP
solution.jsRuns in your browser
Loading editor…
Tests4 to pass
SET_FIELD updates value
SET_ERROR and CLEAR_ERROR work together
SUBMIT sets submitting flag
RESET returns clean state

Run your code to check these.