Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

fieldSubscriptions.lib.ts

30 lines839 Bytes

1import { useSelector } from '@tanstack/react-store'
2import { useMemo } from 'react'
3import type { AnyInternalFieldApi } from '@tanstack/form-core/internals'
4​
5export function useValueFieldSubscription(
6 fieldApi: AnyInternalFieldApi,
7): AnyInternalFieldApi {
8 const state = useSelector(fieldApi.atom, (state) => state)
9​
10 return useMemo(() => {
11 void state
12 return Object.create(fieldApi)
13 }, [fieldApi, state])
15​
16export function useArrayFieldSubscription(
17 fieldApi: AnyInternalFieldApi,
18): AnyInternalFieldApi {
19 const length = useSelector(fieldApi.atom, (state) => state.value.length)
20 const arrayVersion = useSelector(
21 fieldApi.atom,
22 (state) => state.meta._arrayVersion,
23 )
24​
25 return useMemo(() => {
26 void length
27 void arrayVersion
28 return Object.create(fieldApi)
29 }, [fieldApi, arrayVersion, length])