Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Fix(useForm): throw multiple warning when getting form values with `m…
Browse files Browse the repository at this point in the history
…on` method
  • Loading branch information
wellyshen committed Mar 21, 2021
1 parent f050bde commit 4f0dbaf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/lucky-phones-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-form": patch
---

Fix(useForm): throw multiple warning when getting form values with `mon` method
2 changes: 2 additions & 0 deletions src/useForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ describe("useForm", () => {
it('should warn mon "values" alone', () => {
const { mon } = renderHelper();
mon("values");
mon("values");
expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(
'💡 react-cool-form > mon: Getting "values" alone might cause unnecessary re-renders. If you know what you\'re doing, please ignore this warning. See: https://react-cool-form.netlify.app/docs/getting-started/form-state#best-practices'
);
Expand Down
5 changes: 4 additions & 1 deletion src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export default <V extends FormValues = FormValues>({
const onResetRef = useLatest(onReset || (() => undefined));
const onSubmitRef = useLatest(onSubmit || (() => undefined));
const onErrorRef = useLatest(onError || (() => undefined));
const hasWarnValues = useRef(false);
const defaultValuesRef = useRef(defaultValues);
const initialStateRef = useRef<FormState<V>>({
values: defaultValuesRef.current,
Expand Down Expand Up @@ -336,10 +337,12 @@ export default <V extends FormValues = FormValues>({
p = target ? `${target}.${p}` : p;

if (callback) {
if (p === "values")
if (p === "values" && !hasWarnValues.current) {
warn(
`💡 react-cool-form > ${methodName}: Getting "values" alone might cause unnecessary re-renders. If you know what you're doing, please ignore this warning. See: https://react-cool-form.netlify.app/docs/getting-started/form-state#best-practices`
);
hasWarnValues.current = true;
}

usedState[p] = true;
}
Expand Down

0 comments on commit 4f0dbaf

Please sign in to comment.