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

Formik का परिचय

Formik एक form-building kit जैसा है जो आपको सभी tools (state tracking, validation, submit handling) handover करता है ताकि आपको हर एक को scratch से बनाना न पड़े।
Syntax
markup
<Formik
  initialValues={{ field: "" }}
  validationSchema={schema}
  onSubmit={values => {
    // handle values
  }}
>
  <Form>
    <Field name="field" />
    <ErrorMessage name="field" />
  </Form>
</Formik>

Formik क्या Provide करता है

Formik एक popular form library है जो values, validation errors, touched fields, और submission state आपके लिए manage करती है, react-hook-form जैसी spirit में similar लेकिन एक <Formik> component उपयोग करने वाली थोड़ी अलग, ज़्यादा declarative API के around बनी।

Note: Formik अक्सर चुना जाता है जब एक project पहले से Yup उपयोग करता है schema-based validation के लिए, क्योंकि दोनों directly integrate होते हैं।
Warning: यह एक npm package है — इसे एक असली project में install किया जाना चाहिए और यह इस CDN-only sandbox में execute नहीं हो सकता।

उदाहरण: What Formik Provides

markup
// Run in your local React project (npm install required)
import { Formik, Form, Field } from 'formik';

function App() {
  return (
    <Formik initialValues={{ email: '' }} onSubmit={values => console.log(values)}>
      <Form>
        <Field name="email" placeholder="Email" />
        <button type="submit">Submit</button>
      </Form>
    </Formik>
  );
}

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

Yup के साथ Validation जोड़ना

Formik naturally Yup के साथ pair होता है, एक schema-validation library, validationSchema prop के through। अपने खुद के if-statements लिखने के बजाय, आप describe करते हैं कि आपके data को किस shape और rules से match करना चाहिए, और Formik automatically change और submit पर वह validation run करता है।

Note: Yup schemas almost English की तरह पढ़ते हैं: yup.string().email().required() exactly वही describe करता है जो expected है।
Warning: validationSchema के बिना, अपने JSX में 'errors.email' reference करना सिर्फ हमेशा undefined होगा — कुछ भी इसे automatically populate नहीं करता।

उदाहरण: Adding Validation with Yup

markup
// Run in your local React project (npm install required)
import { Formik, Form, Field, ErrorMessage } from 'formik';
import * as yup from 'yup';

const schema = yup.object({ email: yup.string().email().required() });

function App() {
  return (
    <Formik initialValues={{ email: '' }} validationSchema={schema} onSubmit={v => console.log(v)}>
      <Form>
        <Field name="email" />
        <ErrorMessage name="email" component="p" />
      </Form>
    </Formik>
  );
}

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

useFormik Hook सीधे उपयोग करना

<Formik> component और JSX children pattern के बजाय, Formik एक useFormik hook भी offer करता है जो values, handleChange, handleSubmit, और errors directly return करता है — react-hook-form या plain useState-based forms लिखने के style के closer।

Note: useFormik चुनें जब आप हर input के JSX पर ज़्यादा manual control चाहें, और <Formik>/<Field> component style चुनें जब आप कम boilerplate चाहें।
Warning: एक ही form में दोनों styles (<Formik> component AND useFormik) mix न करें — एक API चुनें और consistent रहें।

उदाहरण: Using the useFormik Hook Directly

markup
// Run in your local React project (npm install required)
import { useFormik } from 'formik';

function App() {
  const formik = useFormik({
    initialValues: { email: '' },
    onSubmit: values => console.log(values),
  });
  return (
    <form onSubmit={formik.handleSubmit}>
      <input name="email" value={formik.values.email} onChange={formik.handleChange} />
      <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. Fields को Formik के <Field> component या render-prop API से wrap न करना, automatic value/onChange wiring खोते हुए।
  2. validationSchema भूल जाना, इसलिए कोई validation run नहीं होती भले ही JSX में errors reference किए गए हों।
  3. Manual useState को Formik की खुद की values के साथ mix करना, same field के लिए दो sources of truth cause करते हुए।
चैप्टर सारांश
  • Formik एक npm form library है जो values, errors, और handlers एक single <Formik> component या useFormik hook के through provide करती है।
  • यह आमतौर पर declarative validationSchema definitions के लिए Yup के साथ pair होती है।
  • <Field> component एक input के लिए name, value, और onChange auto-wire करता है।
  • onSubmit final validated values object receive करता है जब form successfully submit हो जाता है।
ब्राउज़र सपोर्ट

npm install formik (और typically yup) चाहिए — इस sandbox में CDN से available नहीं; React 16.8+ के साथ काम करता है।

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.