Skip to content

fieldState

fieldState returns one field’s value and the error to show.

const { value, error, invalid } = fieldState(state, name, options?)
  • state: a SessionState.
  • name: the question name.
  • options.reveal (optional): run client checks. Default false. Your view decides when: on blur, or a Continue attempt.
  • options.messages (optional): copy overrides. See messages.

A FieldState: { value, error, invalid }.

  • A server error wins and suppresses client checks.
  • An unknown name is empty and valid.

Deep equality. Pass it as session.select’s equals, so a field re-renders only when its own state changes.


The client checks on their own. Returns a message or undefined.

Order: required, then length, email, US phone, zip, number (integer, min, max), date bounds and choice membership. The message is the question’s invalidMessage, else the default copy. info never fails.

One field’s server error: { message, invalid }. When an error spans several fields, the first gets the message; the others are marked invalid.


  • MESSAGES: the default English copy.
  • resolveMessages(overrides): merge your own copy over the defaults. This is the i18n hook.
  • formatMessage(template, { min, max }): fill {min} and {max}.
session.select(
(s) => fieldState(s, name, { reveal: touched.has(name) }),
({ error, invalid }) => updateField(name, error, invalid),
sameFieldState,
);
const messages = resolveMessages({ required: 'This one is required' });
fieldState(state, name, { reveal: true, messages });