File tree Expand file tree Collapse file tree 2 files changed +53
-6
lines changed Expand file tree Collapse file tree 2 files changed +53
-6
lines changed Original file line number Diff line number Diff line change @@ -997,11 +997,6 @@ export class FieldApi<
997
997
this . form = opts . form as never
998
998
this . name = opts . name as never
999
999
this . timeoutIds = { } as Record < ValidationCause , never >
1000
- if ( opts . defaultValue !== undefined ) {
1001
- this . form . setFieldValue ( this . name , opts . defaultValue as never , {
1002
- dontUpdateMeta : true ,
1003
- } )
1004
- }
1005
1000
1006
1001
this . store = new Derived ( {
1007
1002
deps : [ this . form . store ] ,
@@ -1072,6 +1067,12 @@ export class FieldApi<
1072
1067
mount = ( ) => {
1073
1068
const cleanup = this . store . mount ( )
1074
1069
1070
+ if ( this . options . defaultValue !== undefined ) {
1071
+ this . form . setFieldValue ( this . name , this . options . defaultValue as never , {
1072
+ dontUpdateMeta : true ,
1073
+ } )
1074
+ }
1075
+
1075
1076
const info = this . getInfo ( )
1076
1077
info . instance = this as never
1077
1078
@@ -1553,7 +1554,6 @@ export class FieldApi<
1553
1554
1554
1555
// TODO: Dedupe this logic to reduce bundle size
1555
1556
for ( const validateObj of validates ) {
1556
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1557
1557
if ( ! validateObj . validate ) continue
1558
1558
validateFieldAsyncFn ( this , validateObj , validatesPromises )
1559
1559
}
Original file line number Diff line number Diff line change @@ -1130,4 +1130,51 @@ describe('useField', () => {
1130
1130
// field2 should not have rerendered
1131
1131
expect ( renderCount . field2 ) . toBe ( field2InitialRender )
1132
1132
} )
1133
+
1134
+ it ( 'should handle defaultValue without setstate-in-render error' , async ( ) => {
1135
+ // Spy on console.error before rendering
1136
+ const consoleErrorSpy = vi . spyOn ( console , 'error' )
1137
+
1138
+ function Comp ( ) {
1139
+ const form = useForm ( {
1140
+ defaultValues : {
1141
+ fieldOne : '' ,
1142
+ fieldTwo : '' ,
1143
+ } ,
1144
+ } )
1145
+
1146
+ const fieldOne = useStore ( form . store , ( state ) => state . values . fieldOne )
1147
+
1148
+ return (
1149
+ < form >
1150
+ < form . Field
1151
+ name = "fieldOne"
1152
+ children = { ( field ) => {
1153
+ return (
1154
+ < input
1155
+ data-testid = { field . name }
1156
+ id = { field . name }
1157
+ value = { field . state . value }
1158
+ onChange = { ( e ) => field . handleChange ( e . target . value ) }
1159
+ />
1160
+ )
1161
+ } }
1162
+ />
1163
+ { fieldOne && (
1164
+ < form . Field
1165
+ name = "fieldTwo"
1166
+ defaultValue = "default field two value"
1167
+ children = { ( _ ) => null }
1168
+ />
1169
+ ) }
1170
+ </ form >
1171
+ )
1172
+ }
1173
+
1174
+ const { getByTestId } = render ( < Comp /> )
1175
+ await user . type ( getByTestId ( 'fieldOne' ) , 'John' )
1176
+
1177
+ // Should not log an error
1178
+ expect ( consoleErrorSpy ) . not . toHaveBeenCalled ( )
1179
+ } )
1133
1180
} )
You can’t perform that action at this time.
0 commit comments