Skip to content

Commit

Permalink
馃崉 close #10195 close async defaultValues not load
Browse files Browse the repository at this point in the history
  • Loading branch information
bluebill1049 committed Mar 30, 2023
1 parent 8c76ad3 commit 1fc89cd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/logic/createFormControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1246,14 +1246,16 @@ export function createFormControl<
};
};

if (isFunction(_options.defaultValues)) {
_options.defaultValues().then((values) => {
reset(values, _options.resetOptions);
_subjects.state.next({
isLoading: false,
const _resetDefaultValues = () => {
if (isFunction(_options.defaultValues)) {
_options.defaultValues().then((values) => {
reset(values, _options.resetOptions);
_subjects.state.next({
isLoading: false,
});
});
});
}
}
};

return {
control: {
Expand All @@ -1268,6 +1270,7 @@ export function createFormControl<
_updateFieldArray,
_getFieldArray,
_reset,
_resetDefaultValues,
_updateFormState,
_subjects,
_proxyFormState,
Expand Down
1 change: 1 addition & 0 deletions src/types/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ export type Control<
_reset: UseFormReset<TFieldValues>;
_options: UseFormProps<TFieldValues, TContext>;
_getDirty: GetIsDirty;
_resetDefaultValues: Noop;
_formState: FormState<TFieldValues>;
_updateValid: (shouldUpdateValid?: boolean) => void;
_updateFormState: (formState: Partial<FormState<TFieldValues>>) => void;
Expand Down
2 changes: 2 additions & 0 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ export function useForm<
React.useEffect(() => {
if (props.values && !deepEqual(props.values, control._defaultValues)) {
control._reset(props.values, control._options.resetOptions);
} else {
control._resetDefaultValues();
}
}, [props.values, control]);

Expand Down

0 comments on commit 1fc89cd

Please sign in to comment.