DEV Community

Nguyễn Hữu Hiếu
Nguyễn Hữu Hiếu

Posted on

1

react-hook-form demo

import { Input } from "@/components/ui/input";
import { Controller, useForm } from "react-hook-form";

const UsernameInput: React.FC<{
  value?: string;
  onChange?: (e?: any) => void;
}> = ({ value, onChange }) => {
  console.log("re-render UsernameInput");
  return (
    <div>
      <label>UsernameInput</label>
      <Input type="text" value={value} onChange={onChange} />
    </div>
  );
};

const MobileInput: React.FC<{
  value?: string;
  onChange?: (e?: any) => void;
}> = ({ value, onChange }) => {
  console.log("re-render MobileInput");
  return (
    <div>
      <label>MobileInput</label>
      <Input type="text" value={value} onChange={onChange} />
    </div>
  );
};

const GmailInput: React.FC<{
  value?: string;
  onChange?: (e?: any) => void;
}> = ({ value, onChange }) => {
  console.log("re-render GmailInput");
  return (
    <div>
      <label>GmailInput</label>
      <Input type="text" value={value} onChange={onChange} />
    </div>
  );
};

export const Demo003HookForm = () => {
  const form = useForm({
    defaultValues: {
      username: "",
      mobile: "",
      gmail: "",
    },
  });

  return (
    <div>
      <h2>Demo003HookForm.tsx</h2>
      <Controller
        name="username"
        control={form.control}
        rules={{ required: true }}
        render={({ field }) => (
          <UsernameInput value={field.value} onChange={field.onChange} />
        )}
      />

      <Controller
        name="mobile"
        control={form.control}
        rules={{ required: true }}
        render={({ field }) => (
          <MobileInput value={field.value} onChange={field.onChange} />
        )}
      />

      <Controller
        name="gmail"
        control={form.control}
        rules={{ required: true }}
        render={({ field }) => (
          <GmailInput value={field.value} onChange={field.onChange} />
        )}
      />
    </div>
  );
};
Enter fullscreen mode Exit fullscreen mode

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more