Custom React Hook

useSafeControl

useSafeControl allows building modular form field components. If a developer explicitly passes control={control}, it uses it; otherwise, it grabs control from the nearest FormProvider context without throwing runtime errors.

Zero crash guarantee: gracefully falls back to useFormContext()
Enables both standalone usage and parent FormProvider wrapping

Hook Parameters

PropertyTypeDefaultDescription
explicitControlControl<any>—Optional control prop passed into component.

Return Object Properties

PropertyTypeDefaultDescription
controlControl<any> | undefined—Resolved valid Control instance.

Practical Hook Examples & Workflows

Explore quickstart usage, full parameter integration, and production data workflows.

Creating a Modular Custom Form Field: Building an input that works with or without explicit control prop.
useSafeControl.custom.tsxtsx
1import React from "react";
2import { useSafeControl } from "antd-dynamic-ui";
3import { Controller, Control } from "react-hook-form";
4import { Input } from "antd";
5
6interface CustomTagInputProps {
7 name: string;
8 label: string;
9 control?: Control<any>;
10}
11
12export default function CustomTagInput({ name, label, control: explicitControl }: CustomTagInputProps) {
13 const control = useSafeControl(explicitControl);
14
15 return (
16 <div className="space-y-1">
17 <label className="text-xs font-semibold">{label}</label>
18 <Controller
19 name={name}
20 control={control}
21 render={({ field }) => (
22 <Input {...field} placeholder="Enter custom tags..." />
23 )}
24 />
25 </div>
26 );
27}