MCPcopy Create free account
hub / github.com/formkit/formkit / useFormKitContextById

Function useFormKitContextById

packages/react/src/composables/useContext.ts:95–137  ·  view source on GitHub ↗
(
  id: string,
  effect?: (context: FormKitFrameworkContext<T>) => void
)

Source from the content-addressed store, hash-verified

93}
94
95export function useFormKitContextById<T = any>(
96 id: string,
97 effect?: (context: FormKitFrameworkContext<T>) => void
98): FormKitFrameworkContext<T> | undefined {
99 const [effectRef] = useState(() => ({
100 current:
101 (effect as ((context: FormKitFrameworkContext<T>) => void) | undefined) ??
102 undefined,
103 }))
104 effectRef.current = effect
105 const [context, setContext] = useState<FormKitFrameworkContext<T> | undefined>(
106 () => getNode<T>(id)?.context as FormKitFrameworkContext<T> | undefined
107 )
108
109 useEffect(() => {
110 const targetNode = getNode<T>(id)
111 if (targetNode) {
112 const next = targetNode.context as FormKitFrameworkContext<T>
113 setContext(next)
114 if (effectRef.current) effectRef.current(next)
115 return
116 }
117
118 const receipt = watchRegistry(id, ({ payload: node }) => {
119 if (node) {
120 const next = node.context as FormKitFrameworkContext<T>
121 stopWatch(receipt)
122 queueMicrotask(() => {
123 setContext(next)
124 if (effectRef.current) effectRef.current(next)
125 })
126 }
127 })
128
129 return () => {
130 stopWatch(receipt)
131 }
132 }, [id])
133
134 useReactiveStore(context)
135
136 return context
137}
138
139export function useFormKitNodeById<T>(
140 id: string,

Callers 4

LiveFormValueFunction · 0.90
ConditionalFieldsFunction · 0.90
HostFunction · 0.50
ChildFunction · 0.50

Calls 4

getNodeFunction · 0.90
watchRegistryFunction · 0.90
stopWatchFunction · 0.90
useReactiveStoreFunction · 0.90

Tested by 2

HostFunction · 0.40
ChildFunction · 0.40