Skip to content

useField

Use primitive field model in react component

Type Signature:

ts
function useField<T extends PrimitiveValue, Meta extends object = any>(
  field: PrimitiveField<T, Meta>,
): ReactPrimitiveFieldApi<T, Meta>

Example Usage:

TIP

You can use useField with field of form for optimization like this:

ts
const form = createForm({ schema: { name: '' } });

// in react component
const { value, onChange } = useField(form.fields.name);
tsx
import { createField } from "@effector-reform/core";
import { useField } from "@effector-reform/react";

const nameField = createField('');

const Field = () => {
  const field = useField(nameField);

  return (
    <>
      <input
        value={field.value}
        onChange={(event) => field.onChange(event.currentTarget.value)}
        onBlur={field.onBlur}
        onFocus={field.onFocus}
      />
    </>
  );
}

API

nametypedescription
valueTfield value
errorFieldErrorfield outer error
metaMetafield meta
isValidbooleanis field valid
onChangeError(newError: FieldError) => voidchange field error
onChange(newValue: T) => voidchange field value
onFocus() => voidfocus field
onBlur() => voidblur field
onChangeMeta(meta: Meta) => voidchange field meta

Released under the MIT License.