API Reference / @gtkx/forms / useFormContext
Variable: useFormContext
constuseFormContext: <TFieldValues,TContext,TTransformedValues>() =>UseFormReturn<TFieldValues,TContext,TTransformedValues>
Defined in: node_modules/.pnpm/react-hook-form@7.86.0_react@19.2.8/node_modules/react-hook-form/dist/useFormContext.d.ts:33
This custom 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 context as a prop. To be used with FormProvider.
Type Parameters
TFieldValues
TFieldValues extends FieldValues
TContext
TContext = any
TTransformedValues
TTransformedValues = TFieldValues
Returns
UseFormReturn<TFieldValues, TContext, TTransformedValues>
return all useForm methods
Remarks
Example
tsx
function App() {
const methods = useForm();
const onSubmit = data => console.log(data);
return (
<FormProvider {...methods} >
<form onSubmit={methods.handleSubmit(onSubmit)}>
<NestedInput />
<input type="submit" />
</form>
</FormProvider>
);
}
function NestedInput() {
const { register } = useFormContext(); // retrieve all hook methods
return <input {...register("test")} />;
}