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

formContext.ts

20 lines540 Bytes

1import { createContext, useContext } from 'solid-js'
2import type { FormApi, Validator } from '@tanstack/form-core'
3​
4type FormContextType =
5 | undefined
6 | {
7 formApi: FormApi<any, Validator<any, unknown> | undefined>
8 parentFieldName?: string
9 }
10​
11export const formContext = createContext<FormContextType>(undefined)
12​
13export function useFormContext() {
14 const formApi: FormContextType = useContext(formContext)
15​
16 if (!formApi)
17 throw new Error(`You are trying to use the form API outside of a form!`)
18​
19 return formApi