MCPcopy Index your code
hub / github.com/react-hook-form/react-hook-form / _subscribe

Function _subscribe

src/logic/createFormControl.ts:1330–1377  ·  view source on GitHub ↗
(props)

Source from the content-addressed store, hash-verified

1328 };
1329
1330 const _subscribe: FromSubscribe<TFieldValues> = (props) => {
1331 const needsValues = !!(props.formState as Record<string, unknown>)?.values;
1332 if (needsValues) {
1333 _valuesSubscriberCount++;
1334 }
1335 const { unsubscribe } = _subjects.state.subscribe({
1336 next: (
1337 formState: Partial<FormState<TFieldValues>> & {
1338 name?: InternalFieldName;
1339 values?: TFieldValues | undefined;
1340 type?: EventType;
1341 },
1342 ) => {
1343 if (
1344 shouldSubscribeByName(props.name, formState.name, props.exact) &&
1345 shouldRenderFormState(
1346 formState,
1347 (props.formState as ReadFormState) || _proxyFormState,
1348 _setFormState,
1349 props.reRenderRoot,
1350 )
1351 ) {
1352 const snapshot = { ..._formValues } as TFieldValues;
1353
1354 props.callback({
1355 values: snapshot,
1356 ..._formState,
1357 ...formState,
1358 defaultValues:
1359 _defaultValues as FormState<TFieldValues>['defaultValues'],
1360 });
1361 }
1362 },
1363 });
1364 if (!needsValues) {
1365 return unsubscribe;
1366 }
1367 let called = false;
1368 return () => {
1369 if (called) {
1370 return;
1371 }
1372
1373 called = true;
1374 _valuesSubscriberCount--;
1375 unsubscribe();
1376 };
1377 };
1378
1379 const subscribe: UseFormSubscribe<TFieldValues> = (props) => {
1380 _state.mount = true;

Callers 1

subscribeFunction · 0.85

Calls 1

unsubscribeFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…