useFormContext
This hook allows you to access the form context. useFormContext is intended to be used in deeply nested structures, where it would become inconvenient to pass the form as a prop.
Returns
This hook will return all the createForm return methods and props.
import { useFormContext } from "solid-hook-form"
export const NestedInput = () => {
const { register } = useFormContext()
return <input {...register("nested")} />
}You need to wrap your form with the FormProvider component for useFormContext to work properly.
import { createForm, FormProvider } from "solid-hook-form"
export const ExampleForm = () => {
const form = createForm({
defaultValues: {
nested: ""
}
})
const { handleSubmit } = form
const onSubmit = (values) => {
console.log(values)
}
return (
<FormProvider form={form}>
<form onSubmit={handleSubmit(onSubmit)}>
<NestedInput />
<button type="submit">Save</button>
</form>
</FormProvider>
)
}