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
| Property | Type | Default | Description |
|---|---|---|---|
| explicitControl | Control<any> | — | Optional control prop passed into component. |
Return Object Properties
| Property | Type | Default | Description |
|---|---|---|---|
| control | Control<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";56interface CustomTagInputProps {7 name: string;8 label: string;9 control?: Control<any>;10}1112export default function CustomTagInput({ name, label, control: explicitControl }: CustomTagInputProps) {13 const control = useSafeControl(explicitControl);1415 return (16 <div className="space-y-1">17 <label className="text-xs font-semibold">{label}</label>18 <Controller19 name={name}20 control={control}21 render={({ field }) => (22 <Input {...field} placeholder="Enter custom tags..." />23 )}24 />25 </div>26 );27}