React Dojo

Search

Search concepts, exercises and quizzes

practiceintermediate

Form with validation

A registration form with three fields (email, password, confirmation). Model the changes and validation with useReducer: each named action, pure reducer, errors derived on each transition.


Objectives

  1. 1.Reducer with actions change, blur, submit
  2. 2.State: { values, touched, submitted } — errors are derived in render, not stored
  3. 3.Validate email (format), password (≥ 6 chars), confirm (equals password)
  4. 4.Only show errors for a field when its touched' or after submit
  5. 5.Submit disabled if there are errors — but computed in render, not stored
  6. 6.On successful submit, show a sent state
your code
Hint

Don't store 'isValid' in state — derive it from errors. Errors are recalculated in the reducer with a pure validate(values) function.

Related concepts

Was this helpful?
Sign in to give feedback