{"id":426870,"date":"2024-07-23T09:00:52","date_gmt":"2024-07-23T09:00:52","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=426870"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=426870","title":{"rendered":"<span>React Hook Form: \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0444\u043e\u0440\u043c \u0434\u043b\u044f \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0445<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em><\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432\u0430\u0436\u043d\u0443\u044e \u0442\u0435\u043c\u0443 \u0434\u043b\u044f \u0432\u0441\u0435\u0445, \u043a\u0442\u043e \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0438 \u043c\u043d\u043e\u0433\u043e\u0448\u0430\u0433\u043e\u0432\u044b\u0445 \u0444\u043e\u0440\u043c \u0432 React. \u041c\u044b \u0432\u0441\u0435 \u0437\u043d\u0430\u0435\u043c, \u043a\u0430\u043a \u044d\u0442\u043e \u0431\u044b\u0432\u0430\u0435\u0442: \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0435 \u0440\u0435\u043d\u0434\u0435\u0440\u044b, \u0442\u043e\u043d\u043d\u044b \u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u0430\u044f \u0431\u043e\u0440\u044c\u0431\u0430 \u0437\u0430 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041d\u043e \u043d\u0438\u043a\u0442\u043e \u0443\u0436\u0435 \u0434\u0430\u0432\u043d\u043e \u043d\u0435 \u043e\u0442\u0447\u0430\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u0432\u0435\u0434\u044c <strong>\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u043e\u0449\u043d\u043e\u0435 \u0438 \u0433\u0438\u0431\u043a\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 <\/strong>\u2014<strong> React Hook Form.<\/strong><\/p>\n<p>React Hook Form \u2014 \u044d\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044e \u043d\u0435\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u0443\u0435\u043c\u044b\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u0445 \u0440\u0435\u043d\u0434\u0435\u0440\u043e\u0432 \u0438 \u043f\u043e\u0432\u044b\u0441\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u0434\u043b\u044f \u043d\u043e\u0432\u0438\u0447\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u043e \u0441\u043b\u043e\u0436\u043d\u044b\u043c\u0438 \u0444\u043e\u0440\u043c\u0430\u043c\u0438 \u0432 React.<\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0444\u043e\u0440\u043c<\/h2>\n<h4>\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0444\u043e\u0440\u043c\u044b \u043d\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b<\/h4>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0444\u043e\u0440\u043c\u044b \u043d\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u2014 \u044d\u0442\u043e \u043e\u0441\u043d\u043e\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e\u0448\u0430\u0433\u043e\u0432\u044b\u0435 \u0444\u043e\u0440\u043c\u044b. \u041a\u0430\u0436\u0434\u044b\u0439 \u0448\u0430\u0433 \u0444\u043e\u0440\u043c\u044b \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u043c:<\/p>\n<pre><code class=\"javascript\">\/\/ Step 1: PersonalInfo.js import React from 'react'; import { useForm } from 'react-hook-form'; import { useHistory } from 'react-router-dom';  const PersonalInfo = ({ onSubmit }) => {   const { register, handleSubmit } = useForm();   const history = useHistory();    const handleNext = (data) => {     onSubmit(data);     history.push('\/employment');   };    return (     &lt;form onSubmit={handleSubmit(handleNext)}>       &lt;div>         &lt;label htmlFor=\"firstName\">First Name&lt;\/label>         &lt;input id=\"firstName\" {...register('firstName', { required: true })} \/>       &lt;\/div>       &lt;div>         &lt;label htmlFor=\"lastName\">Last Name&lt;\/label>         &lt;input id=\"lastName\" {...register('lastName', { required: true })} \/>       &lt;\/div>       &lt;button type=\"submit\">Next&lt;\/button>     &lt;\/form>   ); };  export default PersonalInfo;<\/code><\/pre>\n<h4>\u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0444\u043e\u0440\u043c\u044b \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445<\/h4>\n<p>\u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0444\u043e\u0440\u043c\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442\u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0444\u043e\u0440\u043c\u044b \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c\u0438. \u042d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>FormProvider<\/code> \u0438 <code>useFormContext<\/code>:<\/p>\n<pre><code class=\"javascript\">\/\/ FormContext.js import React, { createContext, useContext, useState } from 'react';  const FormContext = createContext();  export const useFormData = () => useContext(FormContext);  export const FormProvider = ({ children }) => {   const [formData, setFormData] = useState({});    const updateFormData = (data) => {     setFormData((prev) => ({ ...prev, ...data }));   };    return (     &lt;FormContext.Provider value={{ formData, updateFormData }}>       {children}     &lt;\/FormContext.Provider>   ); };<\/code><\/pre>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0448\u0430\u0433\u043e\u0432 \u0444\u043e\u0440\u043c\u044b \u0438 \u0438\u0445 \u0441\u0432\u044f\u0437\u043a\u0430 \u0447\u0435\u0440\u0435\u0437 React Router<\/h4>\n<p>\u0427\u0435\u0440\u0435\u0437<a href=\"https:\/\/reactrouter.com\/en\/main\"> React Router<\/a> \u043c\u043e\u0436\u043d\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0435\u0439 \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u0448\u0430\u0433\u0430\u043c\u0438 \u0444\u043e\u0440\u043c\u044b:<\/p>\n<pre><code class=\"javascript\">\/\/ App.js import React from 'react'; import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; import { FormProvider } from '.\/FormContext'; import PersonalInfo from '.\/PersonalInfo'; import Employment from '.\/Employment'; import Review from '.\/Review';  const App = () => (   &lt;Router>     &lt;FormProvider>       &lt;Switch>         &lt;Route path=\"\/\" exact component={PersonalInfo} \/>         &lt;Route path=\"\/employment\" component={Employment} \/>         &lt;Route path=\"\/review\" component={Review} \/>       &lt;\/Switch>     &lt;\/FormProvider>   &lt;\/Router> );  export default App;<\/code><\/pre>\n<pre><code class=\"javascript\">\/\/ Step 2: Employment.js import React from 'react'; import { useForm } from 'react-hook-form'; import { useHistory } from 'react-router-dom'; import { useFormData } from '.\/FormContext';  const Employment = () => {   const { register, handleSubmit } = useForm();   const history = useHistory();   const { updateFormData } = useFormData();    const handleNext = (data) => {     updateFormData(data);     history.push('\/review');   };    return (     &lt;form onSubmit={handleSubmit(handleNext)}>       &lt;div>         &lt;label htmlFor=\"company\">Company&lt;\/label>         &lt;input id=\"company\" {...register('company', { required: true })} \/>       &lt;\/div>       &lt;div>         &lt;label htmlFor=\"position\">Position&lt;\/label>         &lt;input id=\"position\" {...register('position', { required: true })} \/>       &lt;\/div>       &lt;button type=\"submit\">Next&lt;\/button>     &lt;\/form>   ); };  export default Employment;<\/code><\/pre>\n<h4>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0444\u043e\u0440\u043c\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0445\u0443\u043a\u043e\u0432<\/h4>\n<p>\u0425\u0443\u043a\u0438 <code>useForm<\/code>, <code>useFormContext<\/code> \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0444\u043e\u0440\u043c\u044b \u0438 \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435:<\/p>\n<pre><code class=\"javascript\">\/\/ Step 3: Review.js import React from 'react'; import { useFormData } from '.\/FormContext';  const Review = () => {   const { formData } = useFormData();    const handleSubmit = () => {     console.log('Form submitted:', formData);     \/\/ Add submission logic here   };    return (     &lt;div>       &lt;h2>Review Your Information&lt;\/h2>       &lt;pre>{JSON.stringify(formData, null, 2)}&lt;\/pre>       &lt;button onClick={handleSubmit}>Submit&lt;\/button>     &lt;\/div>   ); };  export default Review; <\/code><\/pre>\n<h3>\u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a<\/h3>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0443 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438.<\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432 <code>required<\/code>, <code>minLength<\/code>, <code>maxLength<\/code>, <code>pattern<\/code>, \u0438 <code>validate<\/code>.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"javascript\">import React from 'react'; import { useForm } from 'react-hook-form';  const SimpleForm = () => {   const { register, handleSubmit, formState: { errors } } = useForm();    const onSubmit = (data) => {     console.log(data);   };    return (     &lt;form onSubmit={handleSubmit(onSubmit)}>       &lt;div>         &lt;label htmlFor=\"name\">Name&lt;\/label>         &lt;input           id=\"name\"           {...register('name', { required: 'Name is required' })}         \/>         {errors.name &amp;&amp; &lt;p>{errors.name.message}&lt;\/p>}       &lt;\/div>       &lt;div>         &lt;label htmlFor=\"email\">Email&lt;\/label>         &lt;input           id=\"email\"           {...register('email', {             required: 'Email is required',             pattern: {               value: \/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$\/i,               message: 'Invalid email address',             },           })}         \/>         {errors.email &amp;&amp; &lt;p>{errors.email.message}&lt;\/p>}       &lt;\/div>       &lt;button type=\"submit\">Submit&lt;\/button>     &lt;\/form>   ); };  export default SimpleForm;<\/code><\/pre>\n<h4>\u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0441 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u043c\u0438 \u0434\u043b\u044f \u0441\u0445\u0435\u043c\u043d\u043e\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 <\/h4>\n<p>\u0414\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u0445\u0430\u0440\u0434\u043e\u0432\u043e\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <a href=\"https:\/\/www.npmjs.com\/package\/yup\">Yup <\/a>\u0438 <a href=\"https:\/\/zod.dev\/\">Zod<\/a>. \u042d\u0442\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0445\u0435\u043c\u044b \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0438 \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u0441 React Hook Form.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0441 Yup:<\/p>\n<pre><code class=\"javascript\">import React from 'react'; import { useForm, Controller } from 'react-hook-form'; import * as yup from 'yup'; import { yupResolver } from '@hookform\/resolvers\/yup';  const schema = yup.object().shape({   name: yup.string().required('Name is required'),   email: yup.string().email('Invalid email').required('Email is required'), });  const YupForm = () => {   const { control, handleSubmit, formState: { errors } } = useForm({     resolver: yupResolver(schema),   });    const onSubmit = (data) => {     console.log(data);   };    return (     &lt;form onSubmit={handleSubmit(onSubmit)}>       &lt;div>         &lt;label htmlFor=\"name\">Name&lt;\/label>         &lt;Controller           name=\"name\"           control={control}           render={({ field }) => &lt;input id=\"name\" {...field} \/>}         \/>         {errors.name &amp;&amp; &lt;p>{errors.name.message}&lt;\/p>}       &lt;\/div>       &lt;div>         &lt;label htmlFor=\"email\">Email&lt;\/label>         &lt;Controller           name=\"email\"           control={control}           render={({ field }) => &lt;input id=\"email\" {...field} \/>}         \/>         {errors.email &amp;&amp; &lt;p>{errors.email.message}&lt;\/p>}       &lt;\/div>       &lt;button type=\"submit\">Submit&lt;\/button>     &lt;\/form>   ); };  export default YupForm;<\/code><\/pre>\n<h4>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043e\u043a<\/h4>\n<p>\u041c\u043e\u0436\u043d\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435\u043c \u043e\u0448\u0438\u0431\u043e\u043a \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <code>errors<\/code>, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0445\u0443\u043a\u0435\u043c <code>useForm<\/code>, \u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"javascript\">import React from 'react'; import { useForm } from 'react-hook-form';  const ErrorHandlingForm = () => {   const { register, handleSubmit, formState: { errors } } = useForm();    const onSubmit = (data) => {     console.log(data);   };    return (     &lt;form onSubmit={handleSubmit(onSubmit)}>       &lt;div>         &lt;label htmlFor=\"username\">Username&lt;\/label>         &lt;input           id=\"username\"           {...register('username', { required: 'Username is required' })}         \/>         {errors.username &amp;&amp; &lt;p>{errors.username.message}&lt;\/p>}       &lt;\/div>       &lt;div>         &lt;label htmlFor=\"password\">Password&lt;\/label>         &lt;input           id=\"password\"           type=\"password\"           {...register('password', {             required: 'Password is required',             minLength: { value: 8, message: 'Password must be at least 8 characters' },           })}         \/>         {errors.password &amp;&amp; &lt;p>{errors.password.message}&lt;\/p>}       &lt;\/div>       &lt;button type=\"submit\">Submit&lt;\/button>     &lt;\/form>   ); };  export default ErrorHandlingForm;<\/code><\/pre>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0434\u043b\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438<\/h4>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0444\u043e\u0440\u043c\u044b \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"javascript\">import React from 'react'; import { useForm } from 'react-hook-form';  const RegistrationForm = () => {   const { register, handleSubmit, formState: { errors } } = useForm();    const onSubmit = (data) => {     console.log('Registration Data:', data);   };    return (     &lt;form onSubmit={handleSubmit(onSubmit)}>       &lt;div>         &lt;label htmlFor=\"firstName\">First Name&lt;\/label>         &lt;input           id=\"firstName\"           {...register('firstName', { required: 'First name is required' })}         \/>         {errors.firstName &amp;&amp; &lt;p>{errors.firstName.message}&lt;\/p>}       &lt;\/div>       &lt;div>         &lt;label htmlFor=\"lastName\">Last Name&lt;\/label>         &lt;input           id=\"lastName\"           {...register('lastName', { required: 'Last name is required' })}         \/>         {errors.lastName &amp;&amp; &lt;p>{errors.lastName.message}&lt;\/p>}       &lt;\/div>       &lt;div>         &lt;label htmlFor=\"email\">Email&lt;\/label>         &lt;input           id=\"email\"           {...register('email', {             required: 'Email is required',             pattern: {               value: \/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$\/i,               message: 'Invalid email address',             },           })}         \/>         {errors.email &amp;&amp; &lt;p>{errors.email.message}&lt;\/p>}       &lt;\/div>       &lt;div>         &lt;label htmlFor=\"password\">Password&lt;\/label>         &lt;input           id=\"password\"           type=\"password\"           {...register('password', {             required: 'Password is required',             minLength: {               value: 8,               message: 'Password must be at least 8 characters',             },           })}         \/>         {errors.password &amp;&amp; &lt;p>{errors.password.message}&lt;\/p>}       &lt;\/div>       &lt;button type=\"submit\">Register&lt;\/button>     &lt;\/form>   ); };  export default RegistrationForm;<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u043f\u0430\u0440\u043e\u043b\u044f:<\/p>\n<pre><code class=\"javascript\">import React from 'react'; import { useForm } from 'react-hook-form';  const CustomValidationForm = () => {   const { register, handleSubmit, formState: { errors } } = useForm();    const validatePassword = (value) => {     if (value.length &lt; 8) {       return 'Password must be at least 8 characters long';     } else if (!\/[A-Z]\/.test(value)) {       return 'Password must contain at least one uppercase letter';     } else if (!\/[0-9]\/.test(value)) {       return 'Password must contain at least one number';     }     return true;   };    const onSubmit = (data) => {     console.log('Form Data:', data);   };    return (     &lt;form onSubmit={handleSubmit(onSubmit)}>       &lt;div>         &lt;label htmlFor=\"password\">Password&lt;\/label>         &lt;input           id=\"password\"           type=\"password\"           {...register('password', {             required: 'Password is required',             validate: validatePassword,           })}         \/>         {errors.password &amp;&amp; &lt;p>{errors.password.message}&lt;\/p>}       &lt;\/div>       &lt;button type=\"submit\">Submit&lt;\/button>     &lt;\/form>   ); };  export default CustomValidationForm;<\/code><\/pre>\n<h2>\u041f\u0440\u043e\u0447\u0438\u0435 \u0444\u0438\u0447\u0438<\/h2>\n<h4>useFieldArray \u0434\u043b\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\/\u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u0435\u0439 \u0444\u043e\u0440\u043c\u044b<\/h4>\n<p><code>useFieldArray<\/code> \u2014 \u044d\u0442\u043e \u043c\u043e\u0449\u043d\u044b\u0439 \u0445\u0443\u043a \u0432 React Hook Form, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u043f\u043e\u043b\u0435\u0439 \u0444\u043e\u0440\u043c\u044b. \u041c\u0430\u0441\u0442\u0445\u0435\u0432 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0444\u043e\u0440\u043c, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u043e\u0440\u043c\u0443, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u043f\u043e\u043b\u044f \u0434\u043b\u044f \u0432\u0432\u043e\u0434\u0430 \u0438\u043c\u0435\u043d \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u043e\u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u044b:<\/p>\n<pre><code class=\"javascript\">import React from 'react'; import { useForm, useFieldArray } from 'react-hook-form';  const TeamForm = () => {   const { register, control, handleSubmit } = useForm({     defaultValues: {       members: [{ name: '' }],     },   });   const { fields, append, remove } = useFieldArray({     control,     name: 'members',   });    const onSubmit = (data) => {     console.log(data);   };    return (     &lt;form onSubmit={handleSubmit(onSubmit)}>       &lt;h2>Team Members&lt;\/h2>       {fields.map((field, index) => (         &lt;div key={field.id}>           &lt;input             {...register(`members.${index}.name`, { required: 'Name is required' })}             defaultValue={field.name}           \/>           &lt;button type=\"button\" onClick={() => remove(index)}>             Remove           &lt;\/button>         &lt;\/div>       ))}       &lt;button type=\"button\" onClick={() => append({ name: '' })}>         Add Member       &lt;\/button>       &lt;button type=\"submit\">Submit&lt;\/button>     &lt;\/form>   ); };  export default TeamForm;<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>useForm<\/code> \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0444\u043e\u0440\u043c\u044b \u0438 <code>useFieldArray<\/code> \u0434\u043b\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u043f\u043e\u043b\u0435\u0439. \u041a\u043d\u043e\u043f\u043a\u0438 &#171;<em>Add Member<\/em>&#187; \u0438 &#171;<em>Remove<\/em>&#187; \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u044e\u0437\u0435\u0440\u0443 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u043f\u043e\u043b\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e.<\/p>\n<h4>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0444\u043e\u0440\u043c<\/h4>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u0444\u043e\u0440\u043c\u044b \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043f\u043e\u043b\u0435\u0439, \u0432\u0430\u0436\u043d\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0438. \u0418 \u0437\u0434\u0435\u0441\u044c \u0443 \u043c\u0435\u043d\u044f \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043e\u0432\u0435\u0442\u043e\u0432:<\/p>\n<ol>\n<li>\n<p><strong>\u0418\u0437\u0431\u0435\u0433\u0430\u0439\u0442\u0435 \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0445 \u0440\u0435\u043d\u0434\u0435\u0440\u043e\u0432<\/strong>: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 <code>React.memo<\/code> \u0438 <code>useMemo<\/code>, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u0445 \u0440\u0435\u043d\u0434\u0435\u0440\u043e\u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u043e\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0444\u043e\u0440\u043c\u044b.<\/p>\n<\/li>\n<li>\n<p><strong>\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u0443\u0435\u043c\u044b\u0435 \u0438 \u043d\u0435\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u0443\u0435\u043c\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b<\/strong>: React Hook Form \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043d\u0435\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u0443\u0435\u043c\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043f\u043e \u0434\u0435\u0444\u043e\u043b\u0442\u0443, \u0447\u0442\u043e \u0441\u043d\u0438\u0436\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0435\u043d\u0434\u0435\u0440\u043e\u0432. \u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u0443\u0435\u043c\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b, \u0443\u0431\u0435\u0436\u0434\u0430\u0435\u043c\u0441\u044f, \u0447\u0442\u043e \u043c\u044b \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0438\u0445 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e.<\/p>\n<\/li>\n<li>\n<p><strong>\u041b\u0435\u043d\u0438\u0432\u0430\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432<\/strong>: \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u0444\u043e\u0440\u043c\u044b \u043f\u043e \u043c\u0435\u0440\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438, \u0430 \u043d\u0435 \u0432\u0441\u0435 \u0441\u0440\u0430\u0437\u0443.<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"javascript\">import React, { memo } from 'react'; import { useForm, Controller } from 'react-hook-form'; import { TextField, Button } from '@material-ui\/core';  const LargeForm = () => {   const { control, handleSubmit } = useForm();      const onSubmit = (data) => {     console.log(data);   };    return (     &lt;form onSubmit={handleSubmit(onSubmit)}>       {Array.from({ length: 100 }).map((_, index) => (         &lt;Controller           key={index}           name={`field${index}`}           control={control}           defaultValue=\"\"           render={({ field }) => &lt;TextField {...field} label={`Field ${index + 1}`} variant=\"outlined\" \/>}         \/>       ))}       &lt;Button type=\"submit\" variant=\"contained\" color=\"primary\">         Submit       &lt;\/Button>     &lt;\/form>   ); };  export default memo(LargeForm);<\/code><\/pre>\n<h4>\u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0441 \u0432\u043d\u0435\u0448\u043d\u0438\u043c\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c\u0438 UI<\/h4>\n<p>React Hook Form \u043b\u0435\u0433\u043a\u043e \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441 \u0432\u043d\u0435\u0448\u043d\u0438\u043c\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u043c\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 UI, \u0442\u0430\u043a\u0438\u043c\u0438 \u043a\u0430\u043a <a href=\"https:\/\/mui.com\/material-ui\/\">Material-UI <\/a>\u0438 <a href=\"https:\/\/ant.design\/\">Ant Design<\/a>. <\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0441 Material-UI:<\/p>\n<pre><code class=\"javascript\">import React from 'react'; import { useForm, Controller } from 'react-hook-form'; import { TextField, Button } from '@material-ui\/core';  const MaterialUIForm = () => {   const { control, handleSubmit } = useForm();    const onSubmit = (data) => {     console.log(data);   };    return (     &lt;form onSubmit={handleSubmit(onSubmit)}>       &lt;Controller         name=\"firstName\"         control={control}         defaultValue=\"\"         render={({ field }) => (           &lt;TextField {...field} label=\"First Name\" variant=\"outlined\" fullWidth margin=\"normal\" \/>         )}       \/>       &lt;Controller         name=\"lastName\"         control={control}         defaultValue=\"\"         render={({ field }) => (           &lt;TextField {...field} label=\"Last Name\" variant=\"outlined\" fullWidth margin=\"normal\" \/>         )}       \/>       &lt;Button type=\"submit\" variant=\"contained\" color=\"primary\">         Submit       &lt;\/Button>     &lt;\/form>   ); };  export default MaterialUIForm;<\/code><\/pre>\n<p>\u0421 React Hook Form \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0444\u043e\u0440\u043c \u0432 React \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0449\u0435!<\/p>\n<hr\/>\n<p><em>\u0412\u0441\u0435 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0438 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u043e\u0441\u0432\u043e\u0438\u0442\u044c \u043d\u0430 \u043e\u043d\u043b\u0430\u0439\u043d-\u043a\u0443\u0440\u0441\u0430\u0445 OTUS: <\/em><a href=\"https:\/\/otus.pw\/m5SV\/\"><em>\u0432 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0435<\/em><\/a><em> \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c, \u0430 <\/em><a href=\"https:\/\/otus.pw\/V3wE\/\"><em>\u0432 \u043a\u0430\u043b\u0435\u043d\u0434\u0430\u0440\u0435<\/em><\/a><em> \u2014 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0435 \u0443\u0440\u043e\u043a\u0438.<\/em><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/829326\/\"> https:\/\/habr.com\/ru\/articles\/829326\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em><\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432\u0430\u0436\u043d\u0443\u044e \u0442\u0435\u043c\u0443 \u0434\u043b\u044f \u0432\u0441\u0435\u0445, \u043a\u0442\u043e \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0438 \u043c\u043d\u043e\u0433\u043e\u0448\u0430\u0433\u043e\u0432\u044b\u0445 \u0444\u043e\u0440\u043c \u0432 React. \u041c\u044b \u0432\u0441\u0435 \u0437\u043d\u0430\u0435\u043c, \u043a\u0430\u043a \u044d\u0442\u043e \u0431\u044b\u0432\u0430\u0435\u0442: \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0435 \u0440\u0435\u043d\u0434\u0435\u0440\u044b, \u0442\u043e\u043d\u043d\u044b \u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u0430\u044f \u0431\u043e\u0440\u044c\u0431\u0430 \u0437\u0430 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041d\u043e \u043d\u0438\u043a\u0442\u043e \u0443\u0436\u0435 \u0434\u0430\u0432\u043d\u043e \u043d\u0435 \u043e\u0442\u0447\u0430\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u0432\u0435\u0434\u044c <strong>\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u043e\u0449\u043d\u043e\u0435 \u0438 \u0433\u0438\u0431\u043a\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 <\/strong>\u2014<strong> React Hook Form.<\/strong><\/p>\n<p>React Hook Form \u2014 \u044d\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044e \u043d\u0435\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u0443\u0435\u043c\u044b\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u0445 \u0440\u0435\u043d\u0434\u0435\u0440\u043e\u0432 \u0438 \u043f\u043e\u0432\u044b\u0441\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u0434\u043b\u044f \u043d\u043e\u0432\u0438\u0447\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u043e \u0441\u043b\u043e\u0436\u043d\u044b\u043c\u0438 \u0444\u043e\u0440\u043c\u0430\u043c\u0438 \u0432 React.<\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0444\u043e\u0440\u043c<\/h2>\n<h4>\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0444\u043e\u0440\u043c\u044b \u043d\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b<\/h4>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0444\u043e\u0440\u043c\u044b \u043d\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u2014 \u044d\u0442\u043e \u043e\u0441\u043d\u043e\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e\u0448\u0430\u0433\u043e\u0432\u044b\u0435 \u0444\u043e\u0440\u043c\u044b. \u041a\u0430\u0436\u0434\u044b\u0439 \u0448\u0430\u0433 \u0444\u043e\u0440\u043c\u044b \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u043c:<\/p>\n<pre><code class=\"javascript\">\/\/ Step 1: PersonalInfo.js import React from 'react'; import { useForm } from 'react-hook-form'; import { useHistory } from 'react-router-dom';  const PersonalInfo = ({ onSubmit }) => {   const { register, handleSubmit } = useForm();   const history = useHistory();    const handleNext = (data) => {     onSubmit(data);     history.push('\/employment');   };    return (     &lt;form onSubmit={handleSubmit(handleNext)}>       &lt;div>         &lt;label htmlFor=\"firstName\">First Name&lt;\/label>         &lt;input id=\"firstName\" {...register('firstName', { required: true })} \/>       &lt;\/div>       &lt;div>         &lt;label htmlFor=\"lastName\">Last Name&lt;\/label>         &lt;input id=\"lastName\" {...register('lastName', { required: true })} \/>       &lt;\/div>       &lt;button type=\"submit\">Next&lt;\/button>     &lt;\/form>   ); };  export default PersonalInfo;<\/code><\/pre>\n<h4>\u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0444\u043e\u0440\u043c\u044b \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445<\/h4>\n<p>\u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0444\u043e\u0440\u043c\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442\u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0444\u043e\u0440\u043c\u044b \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c\u0438. \u042d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>FormProvider<\/code> \u0438 <code>useFormContext<\/code>:<\/p>\n<pre><code class=\"javascript\">\/\/ FormContext.js import React, { createContext, useContext, useState } from 'react';  const FormContext = createContext();  export const useFormData = () => useContext(FormContext);  export const FormProvider = ({ children }) => {   const [formData, setFormData] = useState({});    const updateFormData = (data) => {     setFormData((prev) => ({ ...prev, ...data }));   };    return (     &lt;FormContext.Provider value={{ formData, updateFormData }}>       {children}     &lt;\/FormContext.Provider>   ); };<\/code><\/pre>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0448\u0430\u0433\u043e\u0432 \u0444\u043e\u0440\u043c\u044b \u0438 \u0438\u0445 \u0441\u0432\u044f\u0437\u043a\u0430 \u0447\u0435\u0440\u0435\u0437 React Router<\/h4>\n<p>\u0427\u0435\u0440\u0435\u0437<a href=\"https:\/\/reactrouter.com\/en\/main\"> React Router<\/a> \u043c\u043e\u0436\u043d\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0435\u0439 \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u0448\u0430\u0433\u0430\u043c\u0438 \u0444\u043e\u0440\u043c\u044b:<\/p>\n<pre><code class=\"javascript\">\/\/ App.js import React from 'react'; import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; import { FormProvider } from '.\/FormContext'; import PersonalInfo from '.\/PersonalInfo'; import Employment from '.\/Employment'; import Review from '.\/Review';  const App = () => (   &lt;Router>     &lt;FormProvider>       &lt;Switch>         &lt;Route path=\"\/\" exact component={PersonalInfo} \/>         &lt;Route path=\"\/employment\" component={Employment} \/>         &lt;Route path=\"\/review\" component={Review} \/>       &lt;\/Switch>     &lt;\/FormProvider>   &lt;\/Router> );  export default App;<\/code><\/pre>\n<pre><code class=\"javascript\">\/\/ Step 2: Employment.js import React from 'react'; import { useForm } from 'react-hook-form'; import { useHistory } from 'react-router-dom'; import { useFormData } from '.\/FormContext';  const Employment = () => {   const { register, handleSubmit } = useForm();   const history = useHistory();   const { updateFormData } = useFormData();    const handleNext = (data) => {     updateFormData(data);     history.push('\/review');   };    return (     &lt;form onSubmit={handleSubmit(handleNext)}>       &lt;div>         &lt;label htmlFor=\"company\">Company&lt;\/label>         &lt;input id=\"company\" {...register('company', { required: true })} \/>       &lt;\/div>       &lt;div>         &lt;label htmlFor=\"position\">Position&lt;\/label>         &lt;input id=\"position\" {...register('position', { required: true })} \/>       &lt;\/div>       &lt;button type=\"submit\">Next&lt;\/button>     &lt;\/form>   ); };  export default Employment;<\/code><\/pre>\n<h4>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0444\u043e\u0440\u043c\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0445\u0443\u043a\u043e\u0432<\/h4>\n<p>\u0425\u0443\u043a\u0438 <code>useForm<\/code>, <code>useFormContext<\/code> \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0444\u043e\u0440\u043c\u044b \u0438 \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435:<\/p>\n<pre><code class=\"javascript\">\/\/ Step 3: Review.js import React from 'react'; import { useFormData } from '.\/FormContext';  const Review = () => {   const { formData } = useFormData();    const handleSubmit = () => {     console.log('Form submitted:', formData);     \/\/ Add submission logic here   };    return (     &lt;div>       &lt;h2>Review Your Information&lt;\/h2>       &lt;pre>{JSON.stringify(formData, null, 2)}&lt;\/pre>       &lt;button onClick={handleSubmit}>Submit&lt;\/button>     &lt;\/div>   ); };  export default Review; <\/code><\/pre>\n<h3>\u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a<\/h3>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0443 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438.<\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432 <code>required<\/code>, <code>minLength<\/code>, <code>maxLength<\/code>, <code>pattern<\/code>, \u0438 <code>validate<\/code>.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"javascript\">import React from 'react'; import { useForm } from 'react-hook-form';  const SimpleForm = () => {   const { register, handleSubmit, formState: { errors } } = useForm();    const onSubmit = (data) => {     console.log(data);   };    return (     &lt;form onSubmit={handleSubmit(onSubmit)}>       &lt;div>         &lt;label htmlFor=\"name\">Name&lt;\/label>         &lt;input           id=\"name\"           {...register('name', { required: 'Name is required' })}         \/>         {errors.name &amp;&amp; &lt;p>{errors.name.message}&lt;\/p>}       &lt;\/div>       &lt;div>         &lt;label htmlFor=\"email\">Email&lt;\/label>         &lt;input           id=\"email\"           {...register('email', {             required: 'Email is required',             pattern: {               value: \/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$\/i,               message: 'Invalid email address',             },           })}         \/>         {errors.email &amp;&amp; &lt;p>{errors.email.message}&lt;\/p>}       &lt;\/div>       &lt;button type=\"submit\">Submit&lt;\/button>     &lt;\/form>   ); };  export default SimpleForm;<\/code><\/pre>\n<h4>\u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0441 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u043c\u0438 \u0434\u043b\u044f \u0441\u0445\u0435\u043c\u043d\u043e\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 <\/h4>\n<p>\u0414\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u0445\u0430\u0440\u0434\u043e\u0432\u043e\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <a href=\"https:\/\/www.npmjs.com\/package\/yup\">Yup <\/a>\u0438 <a href=\"https:\/\/zod.dev\/\">Zod<\/a>. \u042d\u0442\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0445\u0435\u043c\u044b \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0438 \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u0441 React Hook Form.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0441 Yup:<\/p>\n<pre><code class=\"javascript\">import React from 'react'; import { useForm, Controller } from 'react-hook-form'; import * as yup from 'yup'; import { yupResolver } from '@hookform\/resolvers\/yup';  const schema = yup.object().shape({   name: yup.string().required('Name is required'),   email: yup.string().email('Invalid email').required('Email is required'), });  const YupForm = () => {   const { control, handleSubmit, formState: { errors } } = useForm({     resolver: yupResolver(schema),   });    const onSubmit = (data) => {     console.log(data);   };    return (     &lt;form onSubmit={handleSubmit(onSubmit)}>       &lt;div>         &lt;label htmlFor=\"name\">Name&lt;\/label>         &lt;Controller           name=\"name\"           control={control}           render={({ field }) => &lt;input id=\"name\" {...field} \/>}         \/>         {errors.name &amp;&amp; &lt;p>{errors.name.message}&lt;\/p>}       &lt;\/div>       &lt;div>         &lt;label htmlFor=\"email\">Email&lt;\/label>         &lt;Controller           name=\"email\"           control={control}           render={({ field }) => &lt;input id=\"email\" {...field} \/>}         \/>         {errors.email &amp;&amp; &lt;p>{errors.email.message}&lt;\/p>}       &lt;\/div>       &lt;button type=\"submit\">Submit&lt;\/button>     &lt;\/form>   ); };  export default YupForm;<\/code><\/pre>\n<h4>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043e\u043a<\/h4>\n<p>\u041c\u043e\u0436\u043d\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435\u043c \u043e\u0448\u0438\u0431\u043e\u043a \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <code>errors<\/code>, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0445\u0443\u043a\u0435\u043c <code>useForm<\/code>, \u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"javascript\">import React from 'react'; import { useForm } from 'react-hook-form';  const ErrorHandlingForm = () => {   const { register, handleSubmit, formState: { errors } } = useForm();    const onSubmit = (data) => {     console.log(data);   };    return (     &lt;form onSubmit={handleSubmit(onSubmit)}>       &lt;div>         &lt;label htmlFor=\"username\">Username&lt;\/label>         &lt;input           id=\"username\"           {...register('username', { required: 'Username is required' })}         \/>         {errors.username &amp;&amp; &lt;p>{errors.username.message}&lt;\/p>}       &lt;\/div>       &lt;div>         &lt;label htmlFor=\"password\">Password&lt;\/label>         &lt;input           id=\"password\"           type=\"password\"           {...register('password', {             required: 'Password is required',             minLength: { value: 8, message: 'Password must be at least 8 characters' },           })}         \/>         {errors.password &amp;&amp; &lt;p>{errors.password.message}&lt;\/p>}       &lt;\/div>       &lt;button type=\"submit\">Submit&lt;\/button>     &lt;\/form>   ); };  export default ErrorHandlingForm;<\/code><\/pre>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0434\u043b\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438<\/h4>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0444\u043e\u0440\u043c\u044b \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"javascript\">import React from 'react'; import { useForm } from 'react-hook-form';  const RegistrationForm = () => {   const { register, handleSubmit, formState: { errors } } = useForm();    const onSubmit = (data) => {     console.log('Registration Data:', data);   };    return (     &lt;form onSubmit={handleSubmit(onSubmit)}>       &lt;div>         &lt;label htmlFor=\"firstName\">First Name&lt;\/label>         &lt;input           id=\"firstName\"           {...register('firstName', { required: 'First name is required' })}         \/>         {errors.firstName &amp;&amp; &lt;p>{errors.firstName.message}&lt;\/p>}       &lt;\/div>       &lt;div>         &lt;label htmlFor=\"lastName\">Last Name&lt;\/label>         &lt;input           id=\"lastName\"           {...register('lastName', { required: 'Last name is required' })}         \/>         {errors.lastName &amp;&amp; &lt;p>{errors.lastName.message}&lt;\/p>}       &lt;\/div>       &lt;div>         &lt;label htmlFor=\"email\">Email&lt;\/label>         &lt;input           id=\"email\"           {...register('email', {             required: 'Email is required',             pattern: {               value: \/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$\/i,               message: 'Invalid email address',             },           })}         \/>         {errors.email &amp;&amp; &lt;p>{errors.email.message}&lt;\/p>}       &lt;\/div>       &lt;div>         &lt;label htmlFor=\"password\">Password&lt;\/label>         &lt;input           id=\"password\"           type=\"password\"           {...register('password', {             required: 'Password is required',<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-426870","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/426870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=426870"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/426870\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=426870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=426870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=426870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}