Skip to content

Commit

Permalink
🤡 fix flush root render hack (react-hook-form#11521)
Browse files Browse the repository at this point in the history
* 🤡 fix flush root render hack

* fix preitter issue
  • Loading branch information
bluebill1049 authored and rafaelcalhau committed May 5, 2024
1 parent c978296 commit f33fe7a
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 55 deletions.
48 changes: 1 addition & 47 deletions src/__tests__/useForm/reset.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import {
import { act, renderHook } from '@testing-library/react-hooks';

import { Controller } from '../../controller';
import {
Control,
UseFormRegister,
UseFormReset,
UseFormReturn,
} from '../../types';
import { Control, UseFormRegister, UseFormReturn } from '../../types';
import { useController } from '../../useController';
import { useFieldArray } from '../../useFieldArray';
import { useForm } from '../../useForm';
Expand Down Expand Up @@ -1279,47 +1274,6 @@ describe('reset', () => {
).toEqual('changed2');
});

it('should allow reset at child level before useForm mounted', () => {
type FormValues = {
firstName: string;
};

const NestChild = ({ reset }: { reset: UseFormReset<FormValues> }) => {
React.useEffect(() => {
reset({
firstName: 'test',
});
}, [reset]);

return null;
};

const Child = ({ reset }: { reset: UseFormReset<FormValues> }) => {
return <NestChild reset={reset} />;
};

function App() {
const { register, reset, handleSubmit } = useForm<FormValues>({
defaultValues: {
firstName: '',
},
});

return (
<form onSubmit={handleSubmit(noop)}>
<input {...register('firstName')} />
<Child reset={reset} />
</form>
);
}

render(<App />);

expect((screen.getByRole('textbox') as HTMLInputElement).value).toEqual(
'test',
);
});

it('should reset field array async', () => {
let tempFields: unknown[] = [];

Expand Down
6 changes: 1 addition & 5 deletions src/logic/createFormControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ export function createFormControl<
TContext = any,
>(
props: UseFormProps<TFieldValues, TContext> = {},
flushRootRender: () => void,
): Omit<UseFormReturn<TFieldValues, TContext>, 'formState'> {
let _options = {
...defaultOptions,
Expand Down Expand Up @@ -657,10 +656,9 @@ export function createFormControl<

isWatched(name, _names) && _subjects.state.next({ ..._formState });
_subjects.values.next({
name,
name: _state.mount ? name : undefined,
values: { ..._formValues },
});
!_state.mount && flushRootRender();
};

const onChange: ChangeHandler = async (event) => {
Expand Down Expand Up @@ -1258,8 +1256,6 @@ export function createFormControl<
focus: '',
};

!_state.mount && flushRootRender();

_state.mount =
!_proxyFormState.isValid ||
!!keepStateOptions.keepIsValid ||
Expand Down
4 changes: 1 addition & 3 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ export function useForm<

if (!_formControl.current) {
_formControl.current = {
...createFormControl(props, () =>
updateFormState((formState) => ({ ...formState })),
),
...createFormControl(props),
formState,
};
}
Expand Down

0 comments on commit f33fe7a

Please sign in to comment.