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

fieldSubscriptions.lib.ts

32 lines939 Bytes

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