react-hook-form 7.23.0-next.1 → 7.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -142,8 +142,10 @@ function useFormState(props) {
142
142
  errors: false,
143
143
  });
144
144
  const _name = React.useRef(name);
145
+ const _mounted = React.useRef(true);
145
146
  _name.current = name;
146
- const callback = React.useCallback((value) => shouldSubscribeByName(_name.current, value.name, exact) &&
147
+ const callback = React.useCallback((value) => _mounted.current &&
148
+ shouldSubscribeByName(_name.current, value.name, exact) &&
147
149
  shouldRenderFormState(value, _localProxyFormState.current) &&
148
150
  updateFormState(Object.assign(Object.assign({}, control._formState), value)), [control, exact]);
149
151
  useSubscribe({
@@ -151,6 +153,9 @@ function useFormState(props) {
151
153
  callback,
152
154
  subject: control._subjects.state,
153
155
  });
156
+ React.useEffect(() => () => {
157
+ _mounted.current = false;
158
+ }, []);
154
159
  return getProxyFormState(formState, control._proxyFormState, _localProxyFormState.current, false);
155
160
  }
156
161
 
@@ -402,14 +407,14 @@ function insert(data, index, value) {
402
407
  }
403
408
 
404
409
  var moveArrayAt = (data, from, to) => {
405
- if (Array.isArray(data)) {
406
- if (isUndefined(data[to])) {
407
- data[to] = undefined;
408
- }
409
- data.splice(to, 0, data.splice(from, 1)[0]);
410
- return data;
410
+ if (!Array.isArray(data)) {
411
+ return [];
411
412
  }
412
- return [];
413
+ if (isUndefined(data[to])) {
414
+ data[to] = undefined;
415
+ }
416
+ data.splice(to, 0, data.splice(from, 1)[0]);
417
+ return data;
413
418
  };
414
419
 
415
420
  function prepend(data, value) {
@@ -537,14 +542,14 @@ const useFieldArray = (props) => {
537
542
  control._updateFieldArray(name, updateAt, {
538
543
  argA: index,
539
544
  argB: value,
540
- }, updatedFieldArrayValues, true, false, false);
545
+ }, updatedFieldArrayValues, true, false);
541
546
  };
542
547
  const replace = (value) => {
543
548
  const updatedFieldArrayValues = convertToArrayPayload(value);
544
549
  ids.current = updatedFieldArrayValues.map(generateId);
545
550
  updateValues([...updatedFieldArrayValues]);
546
551
  setFields([...updatedFieldArrayValues]);
547
- control._updateFieldArray(name, () => updatedFieldArrayValues, {}, [...updatedFieldArrayValues], true, false, false);
552
+ control._updateFieldArray(name, () => updatedFieldArrayValues, {}, [...updatedFieldArrayValues], true, false);
548
553
  };
549
554
  React.useEffect(() => {
550
555
  control._stateFlags.action = false;
@@ -1140,24 +1145,25 @@ function createFormControl(props = {}) {
1140
1145
  }
1141
1146
  return isValid;
1142
1147
  };
1143
- const _updateFieldArray = (name, method, args, values = [], shouldSetValues = true, shouldSetFields = true, shouldSetError = true) => {
1148
+ const _updateFieldArray = (name, method, args, values = [], shouldSetValues = true, shouldSetFields = true) => {
1144
1149
  _stateFlags.action = true;
1145
- if (shouldSetFields && get(_fields, name)) {
1150
+ if (shouldSetFields && Array.isArray(get(_fields, name))) {
1146
1151
  const fieldValues = method(get(_fields, name), args.argA, args.argB);
1147
1152
  shouldSetValues && set(_fields, name, fieldValues);
1148
1153
  }
1149
- if (shouldSetError && Array.isArray(get(_formState.errors, name))) {
1154
+ if (_proxyFormState.errors &&
1155
+ shouldSetFields &&
1156
+ Array.isArray(get(_formState.errors, name))) {
1150
1157
  const errors = method(get(_formState.errors, name), args.argA, args.argB);
1151
1158
  shouldSetValues && set(_formState.errors, name, errors);
1152
1159
  unsetEmptyArray(_formState.errors, name);
1153
1160
  }
1154
- if (_proxyFormState.touchedFields && get(_formState.touchedFields, name)) {
1161
+ if (_proxyFormState.touchedFields &&
1162
+ Array.isArray(get(_formState.touchedFields, name))) {
1155
1163
  const touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
1156
- shouldSetValues &&
1157
- set(_formState.touchedFields, name, touchedFields);
1158
- unsetEmptyArray(_formState.touchedFields, name);
1164
+ shouldSetValues && set(_formState.touchedFields, name, touchedFields);
1159
1165
  }
1160
- if (_proxyFormState.dirtyFields || _proxyFormState.isDirty) {
1166
+ if (_proxyFormState.dirtyFields) {
1161
1167
  _formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
1162
1168
  }
1163
1169
  _subjects.state.next({
@@ -1504,7 +1510,6 @@ function createFormControl(props = {}) {
1504
1510
  : (_formState.errors = {});
1505
1511
  _subjects.state.next({
1506
1512
  errors: _formState.errors,
1507
- isValid: true,
1508
1513
  });
1509
1514
  };
1510
1515
  const setError = (name, error, options) => {