File tree 2 files changed +28
-2
lines changed
2 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -112,11 +112,14 @@ function useField<FormValues: FormValuesShape>(
112
112
113
113
React . useEffect (
114
114
( ) =>
115
- register ( ( state ) => {
115
+ register ( ( newState ) => {
116
116
if ( firstRender . current ) {
117
117
firstRender . current = false ;
118
+ if ( state . initial != newState . initial ) {
119
+ setState ( newState ) ;
120
+ }
118
121
} else {
119
- setState ( state ) ;
122
+ setState ( newState ) ;
120
123
}
121
124
} , false ) ,
122
125
// eslint-disable-next-line react-hooks/exhaustive-deps
Original file line number Diff line number Diff line change @@ -455,4 +455,27 @@ describe("useField", () => {
455
455
expect ( spy . mock . calls [ 3 ] [ 1 ] ) . toBe ( spy . mock . calls [ 2 ] [ 1 ] ) ; // onFocus
456
456
expect ( spy . mock . calls [ 3 ] [ 2 ] ) . toBe ( spy . mock . calls [ 2 ] [ 2 ] ) ; // onBlur
457
457
} ) ;
458
+
459
+ it ( "should listen to initial value 2" , ( ) => {
460
+ const MyFieldListener = ( ) => {
461
+ const isFirstRender = React . useRef ( true )
462
+ const { input, meta } = useField ( "name" ) ;
463
+ if ( ! isFirstRender . current ) {
464
+ expect ( meta . initial ) . toBe ( "test" ) ;
465
+ // expect(input.value).toBe("test");
466
+ }
467
+ isFirstRender . current = false
468
+ return null ;
469
+ } ;
470
+ render (
471
+ < Form onSubmit = { onSubmitMock } >
472
+ { ( ) => (
473
+ < form >
474
+ < MyFieldListener />
475
+ < Field name = "name" component = "input" data-testid = "name" initialValue = "test" />
476
+ </ form >
477
+ ) }
478
+ </ Form > ,
479
+ ) ;
480
+ } ) ;
458
481
} ) ;
You can’t perform that action at this time.
0 commit comments