MCPcopy
hub / github.com/react-hook-form/react-hook-form / _setValue

Function _setValue

src/logic/createFormControl.ts:881–945  ·  view source on GitHub ↗
(
    name: TFieldName,
    value: FieldPathValue<TFieldValues, TFieldName>,
    options: SetValueConfig,
    skipClone: boolean,
    skipStateEmit = false,
  )

Source from the content-addressed store, hash-verified

879 };
880
881 const _setValue = <
882 TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
883 >(
884 name: TFieldName,
885 value: FieldPathValue<TFieldValues, TFieldName>,
886 options: SetValueConfig,
887 skipClone: boolean,
888 skipStateEmit = false,
889 ) => {
890 const field = get(_fields, name);
891 const isFieldArray = _names.array.has(name);
892 const cloneValue = skipClone ? value : cloneObject(value);
893 const previousValue = get(_formValues, name);
894 const isValueUnchanged = deepEqual(previousValue, cloneValue);
895
896 if (!isValueUnchanged) {
897 set(_formValues, name, cloneValue);
898 }
899
900 if (isFieldArray) {
901 _subjects.array.next({
902 name,
903 values: skipClone ? _formValues : cloneObject(_formValues),
904 });
905
906 if (
907 (_proxyFormState.isDirty ||
908 _proxyFormState.dirtyFields ||
909 _proxySubscribeFormState.isDirty ||
910 _proxySubscribeFormState.dirtyFields) &&
911 options.shouldDirty
912 ) {
913 _updateDirtyFields();
914
915 if (!skipStateEmit) {
916 _subjects.state.next({
917 name,
918 dirtyFields: _formState.dirtyFields,
919 isDirty: _getDirty(name, cloneValue),
920 });
921 }
922 }
923 } else {
924 const isEmpty =
925 (Array.isArray(cloneValue) && !cloneValue.length) ||
926 isEmptyObject(cloneValue);
927
928 if (!field || field._f || isNullOrUndefined(cloneValue) || isEmpty) {
929 setFieldValue(name, cloneValue, options, skipClone, skipStateEmit);
930 } else {
931 setFieldValues(name, cloneValue, options, skipClone, skipStateEmit);
932 }
933 }
934
935 if (!isValueUnchanged && !skipStateEmit) {
936 const watched = isWatched(name, _names);
937 const values = skipClone ? _formValues : cloneObject(_formValues);
938

Callers 2

setValueFunction · 0.85
setValuesFunction · 0.85

Calls 6

cloneObjectFunction · 0.85
deepEqualFunction · 0.85
_updateDirtyFieldsFunction · 0.85
_getDirtyFunction · 0.85
setFieldValueFunction · 0.85
setFieldValuesFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…