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

Function ReactFinalForm

src/ReactFinalForm.tsx:40–197  ·  view source on GitHub ↗
({
  debug,
  decorators = [],
  destroyOnUnregister,
  form: alternateFormApi,
  initialValues,
  initialValuesEqual,
  keepDirtyOnReinitialize,
  mutators,
  onSubmit,
  subscription = all,
  validate,
  validateOnBlur,
  ...rest
}: FormProps<FormValues>)

Source from the content-addressed store, hash-verified

38);
39
40function ReactFinalForm<FormValues = Record<string, any>>({
41 debug,
42 decorators = [],
43 destroyOnUnregister,
44 form: alternateFormApi,
45 initialValues,
46 initialValuesEqual,
47 keepDirtyOnReinitialize,
48 mutators,
49 onSubmit,
50 subscription = all,
51 validate,
52 validateOnBlur,
53 ...rest
54}: FormProps<FormValues>) {
55 const config: Config<FormValues> = {
56 debug,
57 destroyOnUnregister,
58 initialValues,
59 keepDirtyOnReinitialize,
60 mutators,
61 onSubmit,
62 validate,
63 validateOnBlur,
64 };
65
66 const form: FormApi<FormValues> = useConstant(() => {
67 const f = alternateFormApi || createForm<FormValues>(config);
68 // pause validation until children register all fields on first render (unpaused in useEffect() below)
69 f.pauseValidation();
70 return f;
71 });
72
73 // Get initial state without triggering callbacks during render
74 const [state, setState] = React.useState<FormState<FormValues>>(() => {
75 // Get initial state synchronously but without callbacks
76 return form.getState();
77 });
78
79 // save a copy of state that can break through the closure
80 // on the shallowEqual() line below.
81 const stateRef = React.useRef<FormState<FormValues>>(state);
82 stateRef.current = state;
83
84 React.useEffect(() => {
85 // We have rendered, so all fields are now registered, so we can unpause validation
86 form.isValidationPaused() && form.resumeValidation();
87 const unsubscriptions: Unsubscribe[] = [
88 form.subscribe((s) => {
89 setState((prevState) => {
90 if (!shallowEqual(s, prevState)) {
91 return s;
92 }
93 return prevState;
94 });
95 }, subscription),
96 ...(decorators ? decorators.map((decorator) => decorator(form)) : []),
97 ];

Callers

nothing calls this directly

Calls 7

addLazyFormStateFunction · 0.90
useConstantFunction · 0.85
shallowEqualFunction · 0.85
decoratorFunction · 0.85
useWhenValueChangesFunction · 0.85
isSyntheticEventFunction · 0.85
renderComponentFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…