MCPcopy
hub / github.com/final-form/react-final-form / register

Function register

src/useField.ts:55–94  ·  view source on GitHub ↗
(
    callback: (state: FieldState<any>) => void,
    silent: boolean,
  )

Source from the content-addressed store, hash-verified

53 const configRef = useLatest(config);
54
55 const register = (
56 callback: (state: FieldState<any>) => void,
57 silent: boolean,
58 ) =>
59 // avoid using `state` const in any closures created inside `register`
60 // because they would refer `state` from current execution context
61 // whereas actual `state` would defined in the subsequent `useField` hook
62 // execution
63 // (that would be caused by `setState` call performed in `register` callback)
64 form.registerField(name as keyof FormValues, callback, subscription, {
65 afterSubmit,
66 beforeSubmit: () => {
67 const {
68 beforeSubmit,
69 formatOnBlur,
70 format = defaultFormat,
71 } = configRef.current;
72
73 if (formatOnBlur) {
74 const fieldState = form.getFieldState(name as keyof FormValues);
75 if (fieldState) {
76 const { value } = fieldState;
77 const formatted = format(value, name);
78
79 if (formatted !== value) {
80 form.change(name as keyof FormValues, formatted);
81 }
82 }
83 }
84
85 return beforeSubmit && beforeSubmit();
86 },
87 data,
88 defaultValue,
89 getValidator: () => configRef.current.validate,
90 initialValue,
91 isEqual: configRef.current.isEqual,
92 silent,
93 validateFields,
94 });
95
96 // Initialize state with proper field state from Final Form without callbacks
97 const [state, setState] = React.useState<FieldState<any>>(() => {

Callers 1

useFieldFunction · 0.85

Calls 1

formatFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…