From 92187d3c08b210685a9038929f535e1000869562 Mon Sep 17 00:00:00 2001 From: Juan Villa Date: Tue, 11 Mar 2025 21:23:48 -0400 Subject: [PATCH 1/3] fix(form-core): move setting default value to mount function to avoid react set state on render error + include test in react package --- packages/form-core/src/FieldApi.ts | 12 +++--- packages/react-form/tests/useField.test.tsx | 47 +++++++++++++++++++++ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/packages/form-core/src/FieldApi.ts b/packages/form-core/src/FieldApi.ts index b136dfc4b..c66a8e93e 100644 --- a/packages/form-core/src/FieldApi.ts +++ b/packages/form-core/src/FieldApi.ts @@ -997,11 +997,6 @@ export class FieldApi< this.form = opts.form as never this.name = opts.name as never this.timeoutIds = {} as Record - if (opts.defaultValue !== undefined) { - this.form.setFieldValue(this.name, opts.defaultValue as never, { - dontUpdateMeta: true, - }) - } this.store = new Derived({ deps: [this.form.store], @@ -1072,6 +1067,12 @@ export class FieldApi< mount = () => { const cleanup = this.store.mount() + if (this.options.defaultValue !== undefined) { + this.form.setFieldValue(this.name, this.options.defaultValue as never, { + dontUpdateMeta: true, + }) + } + const info = this.getInfo() info.instance = this as never @@ -1553,7 +1554,6 @@ export class FieldApi< // TODO: Dedupe this logic to reduce bundle size for (const validateObj of validates) { - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (!validateObj.validate) continue validateFieldAsyncFn(this, validateObj, validatesPromises) } diff --git a/packages/react-form/tests/useField.test.tsx b/packages/react-form/tests/useField.test.tsx index a5969c4e8..0728322cf 100644 --- a/packages/react-form/tests/useField.test.tsx +++ b/packages/react-form/tests/useField.test.tsx @@ -1130,4 +1130,51 @@ describe('useField', () => { // field2 should not have rerendered expect(renderCount.field2).toBe(field2InitialRender) }) + + it('should handle defaultValue without setstate-in-render error', async () => { + // Spy on console.error before rendering + const consoleErrorSpy = vi.spyOn(console, 'error') + + function Comp() { + const form = useForm({ + defaultValues: { + fieldOne: '', + fieldTwo: '', + }, + }) + + const fieldOne = useStore(form.store, (state) => state.values.fieldOne) + + return ( +
+ { + return ( + field.handleChange(e.target.value)} + /> + ) + }} + /> + {fieldOne && ( + null} + /> + )} + + ) + } + + const { getByTestId } = render() + await user.type(getByTestId('fieldOne'), 'John') + + // Should not log an error + expect(consoleErrorSpy).not.toHaveBeenCalled() + }) }) From a2d03f91c89b30e60cf5bd1d73cf7dc2e93cff37 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Wed, 12 Mar 2025 01:35:56 +0000 Subject: [PATCH 2/3] ci: apply automated fixes and generate docs --- docs/reference/classes/fieldapi.md | 40 +++++++++++++++--------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/docs/reference/classes/fieldapi.md b/docs/reference/classes/fieldapi.md index 8026b374d..fbd1f62d5 100644 --- a/docs/reference/classes/fieldapi.md +++ b/docs/reference/classes/fieldapi.md @@ -163,7 +163,7 @@ The current field state. getInfo(): FieldInfo ``` -Defined in: [packages/form-core/src/FieldApi.ts:1216](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1216) +Defined in: [packages/form-core/src/FieldApi.ts:1222](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1222) Gets the field information object. @@ -179,7 +179,7 @@ Gets the field information object. getMeta(): FieldMeta ``` -Defined in: [packages/form-core/src/FieldApi.ts:1184](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1184) +Defined in: [packages/form-core/src/FieldApi.ts:1190](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1190) #### Returns @@ -193,7 +193,7 @@ Defined in: [packages/form-core/src/FieldApi.ts:1184](https://github.com/TanStac getValue(): TData ``` -Defined in: [packages/form-core/src/FieldApi.ts:1166](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1166) +Defined in: [packages/form-core/src/FieldApi.ts:1172](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1172) Gets the current field value. @@ -249,12 +249,12 @@ Handles the change event. ```ts insertValue( - index, - value, + index, + value, opts?): void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1236](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1236) +Defined in: [packages/form-core/src/FieldApi.ts:1242](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1242) Inserts a value at the specified index, shifting the subsequent values to the right. @@ -302,12 +302,12 @@ Mounts the field instance to the form. ```ts moveValue( - aIndex, - bIndex, + aIndex, + bIndex, opts?): void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1292](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1292) +Defined in: [packages/form-core/src/FieldApi.ts:1298](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1298) Moves the value at the first specified index to the second specified index. @@ -337,7 +337,7 @@ Moves the value at the first specified index to the second specified index. pushValue(value, opts?): void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1221](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1221) +Defined in: [packages/form-core/src/FieldApi.ts:1227](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1227) Pushes a new value to the field. @@ -363,7 +363,7 @@ Pushes a new value to the field. removeValue(index, opts?): void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1268](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1268) +Defined in: [packages/form-core/src/FieldApi.ts:1274](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1274) Removes a value at the specified index. @@ -387,12 +387,12 @@ Removes a value at the specified index. ```ts replaceValue( - index, - value, + index, + value, opts?): void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1252](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1252) +Defined in: [packages/form-core/src/FieldApi.ts:1258](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1258) Replaces a value at the specified index. @@ -444,7 +444,7 @@ Updates the field's errorMap setMeta(updater): void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1189](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1189) +Defined in: [packages/form-core/src/FieldApi.ts:1195](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1195) Sets the field metadata. @@ -466,7 +466,7 @@ Sets the field metadata. setValue(updater, options?): void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1173](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1173) +Defined in: [packages/form-core/src/FieldApi.ts:1179](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1179) Sets the field value and run the `change` validator. @@ -490,12 +490,12 @@ Sets the field value and run the `change` validator. ```ts swapValues( - aIndex, - bIndex, + aIndex, + bIndex, opts?): void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1280](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1280) +Defined in: [packages/form-core/src/FieldApi.ts:1286](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1286) Swaps the values at the specified indices. @@ -525,7 +525,7 @@ Swaps the values at the specified indices. update(opts): void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1114](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1114) +Defined in: [packages/form-core/src/FieldApi.ts:1120](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1120) Updates the field instance with new options. From 6b912195a0be05f652d2d4fc3084a2e0087aef6b Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Thu, 20 Mar 2025 11:17:41 +0000 Subject: [PATCH 3/3] ci: apply automated fixes and generate docs (attempt 2/3) --- docs/reference/classes/fieldapi.md | 42 +++++++++++++++--------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/docs/reference/classes/fieldapi.md b/docs/reference/classes/fieldapi.md index fbd1f62d5..10df30253 100644 --- a/docs/reference/classes/fieldapi.md +++ b/docs/reference/classes/fieldapi.md @@ -163,7 +163,7 @@ The current field state. getInfo(): FieldInfo ``` -Defined in: [packages/form-core/src/FieldApi.ts:1222](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1222) +Defined in: [packages/form-core/src/FieldApi.ts:1217](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1217) Gets the field information object. @@ -179,7 +179,7 @@ Gets the field information object. getMeta(): FieldMeta ``` -Defined in: [packages/form-core/src/FieldApi.ts:1190](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1190) +Defined in: [packages/form-core/src/FieldApi.ts:1185](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1185) #### Returns @@ -193,7 +193,7 @@ Defined in: [packages/form-core/src/FieldApi.ts:1190](https://github.com/TanStac getValue(): TData ``` -Defined in: [packages/form-core/src/FieldApi.ts:1172](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1172) +Defined in: [packages/form-core/src/FieldApi.ts:1167](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1167) Gets the current field value. @@ -249,12 +249,12 @@ Handles the change event. ```ts insertValue( - index, - value, + index, + value, opts?): void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1242](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1242) +Defined in: [packages/form-core/src/FieldApi.ts:1237](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1237) Inserts a value at the specified index, shifting the subsequent values to the right. @@ -284,7 +284,7 @@ Inserts a value at the specified index, shifting the subsequent values to the ri mount(): () => void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1072](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1072) +Defined in: [packages/form-core/src/FieldApi.ts:1067](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1067) Mounts the field instance to the form. @@ -302,12 +302,12 @@ Mounts the field instance to the form. ```ts moveValue( - aIndex, - bIndex, + aIndex, + bIndex, opts?): void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1298](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1298) +Defined in: [packages/form-core/src/FieldApi.ts:1293](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1293) Moves the value at the first specified index to the second specified index. @@ -337,7 +337,7 @@ Moves the value at the first specified index to the second specified index. pushValue(value, opts?): void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1227](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1227) +Defined in: [packages/form-core/src/FieldApi.ts:1222](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1222) Pushes a new value to the field. @@ -363,7 +363,7 @@ Pushes a new value to the field. removeValue(index, opts?): void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1274](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1274) +Defined in: [packages/form-core/src/FieldApi.ts:1269](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1269) Removes a value at the specified index. @@ -387,12 +387,12 @@ Removes a value at the specified index. ```ts replaceValue( - index, - value, + index, + value, opts?): void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1258](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1258) +Defined in: [packages/form-core/src/FieldApi.ts:1253](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1253) Replaces a value at the specified index. @@ -444,7 +444,7 @@ Updates the field's errorMap setMeta(updater): void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1195](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1195) +Defined in: [packages/form-core/src/FieldApi.ts:1190](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1190) Sets the field metadata. @@ -466,7 +466,7 @@ Sets the field metadata. setValue(updater, options?): void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1179](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1179) +Defined in: [packages/form-core/src/FieldApi.ts:1174](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1174) Sets the field value and run the `change` validator. @@ -490,12 +490,12 @@ Sets the field value and run the `change` validator. ```ts swapValues( - aIndex, - bIndex, + aIndex, + bIndex, opts?): void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1286](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1286) +Defined in: [packages/form-core/src/FieldApi.ts:1281](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1281) Swaps the values at the specified indices. @@ -525,7 +525,7 @@ Swaps the values at the specified indices. update(opts): void ``` -Defined in: [packages/form-core/src/FieldApi.ts:1120](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1120) +Defined in: [packages/form-core/src/FieldApi.ts:1115](https://github.com/TanStack/form/blob/main/packages/form-core/src/FieldApi.ts#L1115) Updates the field instance with new options.