← Back to React Course | Chapter 7: Forms & User Input | Lesson 4 of 8

react-hook-form का उपयोग

react-hook-form एक personal assistant जैसा है जो आपके सभी form fields track करता है ताकि आपको हर single एक के लिए useState न लिखना पड़े।
Syntax
markup
const { register, handleSubmit, formState: { errors } } = useForm();

<form onSubmit={handleSubmit(onSubmit)}>
  <input {...register("fieldName", { required: "Message" })} />
  {errors.fieldName && <p>{errors.fieldName.message}</p>}
</form>

एक Form Library क्यों उपयोग करें

कई fields वाले forms के लिए, हर single एक के लिए एक useState और onChange handler लिखना repetitive हो जाता है और हर keystroke पर unnecessary re-renders cause कर सकता है। react-hook-form uncontrolled inputs उपयोग करके internally सभी field state manage करता है, एक simple register() function expose करते हुए।

Note: react-hook-form खासकर बड़े forms (10+ fields) के लिए popular है जहां performance और boilerplate दोनों मायने रखते हैं।
Warning: यह एक npm package है, React में built-in नहीं — इसे install करने की ज़रूरत है और यह इस CDN-only preview sandbox में run नहीं हो सकता।

उदाहरण: Why Use a Form Library

markup
// Run in your local React project (npm install required)
import { useForm } from 'react-hook-form';

function App() {
  const { register, handleSubmit } = useForm();
  const onSubmit = data => console.log(data);
  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input {...register('email')} placeholder="Email" />
      <button type="submit">Submit</button>
    </form>
  );
}

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

register() के साथ Inputs Register करना

register(fieldName) call करना वे props (ref, onChange, onBlur, name) return करता है जो form द्वारा track होने के लिए एक input को चाहिए होते हैं, आपके हाथ से लिखे बिना। इसके return value को एक <input> पर spread करना उस field को automatically form में wire करता है।

Note: आप validation rules सीधे register में pass कर सकते हैं, जैसे register(email, { required: true })।
Warning: Spread syntax ({...register(email)}) भूल जाने का मतलब है सिर्फ कुछ required props attach होते हैं, tracking को चुपचाप तोड़ते हुए।

उदाहरण: Registering Inputs with register()

markup
// Run in your local React project (npm install required)
import { useForm } from 'react-hook-form';

function App() {
  const { register, handleSubmit } = useForm();
  return (
    <form onSubmit={handleSubmit(data => console.log(data))}>
      <input {...register('username', { required: true })} />
      <input {...register('age', { min: 18 })} type="number" />
      <button type="submit">Submit</button>
    </form>
  );
}

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

formState से Validation Errors पढ़ना

जब आप register() में validation rules जोड़ते हैं, react-hook-form automatically formState.errors में कोई भी failures track करता है, field name से keyed।

formState.errors को register और handleSubmit के साथ destructure करना inline error messages दिखाने के लिए ज़रूरी सब कुछ देता है।

Note: हर input के नीचे errors.fieldName && <message> check करें error को उस field के पास रखने के लिए जिससे यह belong करता है।
Warning: formState.errors resets timing-sensitive हैं — हमेशा इसे same useForm() call से पढ़ें, इसे अलग local state में कभी store न करें।

उदाहरण: Reading Validation Errors from formState

markup
// Run in your local React project (npm install required)
import { useForm } from 'react-hook-form';

function App() {
  const { register, handleSubmit, formState: { errors } } = useForm();
  return (
    <form onSubmit={handleSubmit(data => console.log(data))}>
      <input {...register('email', { required: 'Email is required' })} />
      {errors.email && <p>{errors.email.message}</p>}
      <button type="submit">Submit</button>
    </form>
  );
}

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Input पर {...register(fieldName)} spread करना भूल जाना, जो इसे form की tracking से disconnect करता है।
  2. react-hook-form के uncontrolled approach को same field के लिए manual useState के साथ mix करना।
  3. formState से errors न पढ़ना, इसलिए validation messages कभी नहीं दिखते।
चैप्टर सारांश
  • react-hook-form internally uncontrolled inputs और refs उपयोग करके form state manage करता है, re-renders minimize करते हुए।
  • register() function per field useState की ज़रूरत के बिना एक input को form में wire करता है।
  • handleSubmit आपके submit function को wrap करता है और सिर्फ validation pass होने पर इसे call करता है।
  • formState.errors field name से keyed validation messages रखता है।
ब्राउज़र सपोर्ट

npm install react-hook-form चाहिए — इस sandbox में CDN से available नहीं; React 16.8+ (internally hooks उपयोग करता है) के साथ काम करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.