@@ -76,6 +76,18 @@ function useWatch<TForm extends FormInstance>(
7676 form ?: TForm | WatchOptions < TForm > ,
7777) : GetGeneric < TForm > ;
7878
79+ // ------- selector type -------
80+ function useWatch < TForm extends FormInstance , TSelected = unknown > (
81+ selector : ( values : GetGeneric < TForm > ) => TSelected ,
82+ form ?: TForm | WatchOptions < TForm > ,
83+ ) : TSelected ;
84+
85+ function useWatch < ValueType = Store , TSelected = unknown > (
86+ selector : ( values : ValueType ) => TSelected ,
87+ form ?: FormInstance | WatchOptions < FormInstance > ,
88+ ) : TSelected ;
89+ // ------- selector type end -------
90+
7991function useWatch < TForm extends FormInstance > (
8092 dependencies : NamePath ,
8193 form ?: TForm | WatchOptions < TForm > ,
@@ -86,8 +98,10 @@ function useWatch<ValueType = Store>(
8698 form ?: FormInstance | WatchOptions < FormInstance > ,
8799) : ValueType ;
88100
89- function useWatch ( ...args : [ NamePath , FormInstance | WatchOptions < FormInstance > ] ) {
90- const [ dependencies = [ ] , _form = { } ] = args ;
101+ function useWatch (
102+ ...args : [ NamePath | ( ( values : Store ) => any ) , FormInstance | WatchOptions < FormInstance > ]
103+ ) {
104+ const [ dependencies , _form = { } ] = args ;
91105 const options = isFormInstance ( _form ) ? { form : _form } : _form ;
92106 const form = options . form ;
93107
@@ -125,8 +139,15 @@ function useWatch(...args: [NamePath, FormInstance | WatchOptions<FormInstance>]
125139 const { getFieldsValue, getInternalHooks } = formInstance ;
126140 const { registerWatch } = getInternalHooks ( HOOK_MARK ) ;
127141
142+ const getWatchValue = ( values : any , allValues : any ) => {
143+ const watchValue = options . preserve ? allValues : values ;
144+ return typeof dependencies === 'function'
145+ ? dependencies ( watchValue )
146+ : getValue ( watchValue , namePathRef . current ) ;
147+ } ;
148+
128149 const cancelRegister = registerWatch ( ( values , allValues ) => {
129- const newValue = getValue ( options . preserve ? allValues : values , namePathRef . current ) ;
150+ const newValue = getWatchValue ( values , allValues ) ;
130151 const nextValueStr = stringify ( newValue ) ;
131152
132153 // Compare stringify in case it's nest object
@@ -137,10 +158,7 @@ function useWatch(...args: [NamePath, FormInstance | WatchOptions<FormInstance>]
137158 } ) ;
138159
139160 // TODO: We can improve this perf in future
140- const initialValue = getValue (
141- options . preserve ? getFieldsValue ( true ) : getFieldsValue ( ) ,
142- namePathRef . current ,
143- ) ;
161+ const initialValue = getWatchValue ( getFieldsValue ( ) , getFieldsValue ( true ) ) ;
144162
145163 // React 18 has the bug that will queue update twice even the value is not changed
146164 // ref: https://github.com/facebook/react/issues/27213
0 commit comments