MCPcopy
hub / github.com/react-hook-form/react-hook-form / watch

Function watch

src/logic/createFormControl.ts:1288–1328  ·  view source on GitHub ↗
(
    name?:
      | FieldPath<TFieldValues>
      | ReadonlyArray<FieldPath<TFieldValues>>
      | WatchObserver<TFieldValues>,
    defaultValue?: DeepPartial<TFieldValues>,
  )

Source from the content-addressed store, hash-verified

1286 };
1287
1288 const watch: UseFormWatch<TFieldValues> = (
1289 name?:
1290 | FieldPath<TFieldValues>
1291 | ReadonlyArray<FieldPath<TFieldValues>>
1292 | WatchObserver<TFieldValues>,
1293 defaultValue?: DeepPartial<TFieldValues>,
1294 ) => {
1295 if (isFunction(name)) {
1296 _valuesSubscriberCount++;
1297 const { unsubscribe } = _subjects.state.subscribe({
1298 next: (payload) =>
1299 'values' in payload &&
1300 name(
1301 payload.values || _getWatch(undefined, defaultValue),
1302 payload as {
1303 name?: FieldPath<TFieldValues>;
1304 type?: EventType;
1305 value?: unknown;
1306 },
1307 ),
1308 });
1309 let called = false;
1310
1311 return {
1312 unsubscribe: () => {
1313 if (called) {
1314 return;
1315 }
1316
1317 called = true;
1318 _valuesSubscriberCount--;
1319 unsubscribe();
1320 },
1321 };
1322 }
1323 return _getWatch(
1324 name as InternalFieldName | InternalFieldName[],
1325 defaultValue,
1326 true,
1327 );
1328 };
1329
1330 const _subscribe: FromSubscribe<TFieldValues> = (props) => {
1331 const needsValues = !!(props.formState as Record<string, unknown>)?.values;

Callers 15

AppFunction · 0.85
AppFunction · 0.85
ComponentFunction · 0.85
ComponentFunction · 0.85
AppFunction · 0.85
ComponentFunction · 0.85
AppFunction · 0.85
FormFunction · 0.85
ComponentFunction · 0.85
AppFunction · 0.85
VisibilityWatchFunction · 0.85
ComponentFunction · 0.85

Calls 2

_getWatchFunction · 0.85
unsubscribeFunction · 0.85

Tested by 15

AppFunction · 0.68
AppFunction · 0.68
ComponentFunction · 0.68
ComponentFunction · 0.68
AppFunction · 0.68
ComponentFunction · 0.68
AppFunction · 0.68
FormFunction · 0.68
ComponentFunction · 0.68
AppFunction · 0.68
VisibilityWatchFunction · 0.68
ComponentFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…