<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaScript / TypeScript - Software Developer's Tour</title>
	<atom:link href="https://pawelmajewski.com/category/programming/javascript-typescript/feed/" rel="self" type="application/rss+xml" />
	<link>https://pawelmajewski.com</link>
	<description>Website about programming, news in IT and more</description>
	<lastBuildDate>Wed, 11 Jun 2025 08:58:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>

<image>
	<url>https://pawelmajewski.com/wp-content/uploads/2024/02/logo_normal.png</url>
	<title>JavaScript / TypeScript - Software Developer's Tour</title>
	<link>https://pawelmajewski.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Simplify Your React Forms with React Hook Form and Yup</title>
		<link>https://pawelmajewski.com/simplify-your-react-forms-with-react-hook-form-and-yup/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=simplify-your-react-forms-with-react-hook-form-and-yup</link>
					<comments>https://pawelmajewski.com/simplify-your-react-forms-with-react-hook-form-and-yup/#respond</comments>
		
		<dc:creator><![CDATA[Paweł Majewski]]></dc:creator>
		<pubDate>Mon, 12 May 2025 09:52:00 +0000</pubDate>
				<category><![CDATA[ReactJS]]></category>
		<category><![CDATA[Custom forms]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[React hook form]]></category>
		<category><![CDATA[ReactJs]]></category>
		<category><![CDATA[Validation]]></category>
		<category><![CDATA[Yup]]></category>
		<guid isPermaLink="false">https://pawelmajewski.com/?p=8788</guid>

					<description><![CDATA[<p>Here we are! The first post about frontend technology has been written. Until now, only backend and DevOps topics have been covered. Today I want to show you one of the top libraries that allows us to manage forms easily. React Hook Form and Yup What is React Hook Form? It&#8217;s one of the most frequently [&#8230;]</p>
<p>The post <a href="https://pawelmajewski.com/simplify-your-react-forms-with-react-hook-form-and-yup/">Simplify Your React Forms with React Hook Form and Yup</a> first appeared on <a href="https://pawelmajewski.com">Software Developer's Tour</a>.</p>]]></description>
										<content:encoded><![CDATA[<div data-elementor-type="wp-post" data-elementor-id="8788" class="elementor elementor-8788">
				<div class="elementor-element elementor-element-eb501a8 e-flex e-con-boxed wpr-particle-no wpr-jarallax-no wpr-parallax-no wpr-sticky-section-no e-con e-parent" data-id="eb501a8" data-element_type="container">
					<div class="e-con-inner">
				<div class="elementor-element elementor-element-24353a1 elementor-widget elementor-widget-text-editor" data-id="24353a1" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
			<style>/*! elementor - v3.21.0 - 08-05-2024 */
.elementor-widget-text-editor.elementor-drop-cap-view-stacked .elementor-drop-cap{background-color:#69727d;color:#fff}.elementor-widget-text-editor.elementor-drop-cap-view-framed .elementor-drop-cap{color:#69727d;border:3px solid;background-color:transparent}.elementor-widget-text-editor:not(.elementor-drop-cap-view-default) .elementor-drop-cap{margin-top:8px}.elementor-widget-text-editor:not(.elementor-drop-cap-view-default) .elementor-drop-cap-letter{width:1em;height:1em}.elementor-widget-text-editor .elementor-drop-cap{float:left;text-align:center;line-height:1;font-size:50px}.elementor-widget-text-editor .elementor-drop-cap-letter{display:inline-block}</style>				<p>Here we are! The first post about frontend technology has been written. Until now, only backend and DevOps topics have been covered.</p>						</div>
				</div>
				<div class="elementor-element elementor-element-6a9add3 elementor-widget elementor-widget-text-editor" data-id="6a9add3" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
							<p>Today I want to show you one of the top libraries that allows us to manage forms easily.</p>						</div>
				</div>
				<div class="elementor-element elementor-element-29205e5 elementor-widget elementor-widget-heading" data-id="29205e5" data-element_type="widget" data-widget_type="heading.default">
				<div class="elementor-widget-container">
			<style>/*! elementor - v3.21.0 - 08-05-2024 */
.elementor-heading-title{padding:0;margin:0;line-height:1}.elementor-widget-heading .elementor-heading-title[class*=elementor-size-]>a{color:inherit;font-size:inherit;line-height:inherit}.elementor-widget-heading .elementor-heading-title.elementor-size-small{font-size:15px}.elementor-widget-heading .elementor-heading-title.elementor-size-medium{font-size:19px}.elementor-widget-heading .elementor-heading-title.elementor-size-large{font-size:29px}.elementor-widget-heading .elementor-heading-title.elementor-size-xl{font-size:39px}.elementor-widget-heading .elementor-heading-title.elementor-size-xxl{font-size:59px}</style><h2 class="elementor-heading-title elementor-size-default">React Hook Form and Yup</h2>		</div>
				</div>
				<div class="elementor-element elementor-element-a9b34d4 elementor-widget elementor-widget-text-editor" data-id="a9b34d4" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
							<p>What is <strong>React Hook Form?</strong></p><p>It&#8217;s one of the most frequently used library to manage forms with <strong>React</strong>. As we can read on the project&#8217;s site </p>						</div>
				</div>
				<div class="elementor-element elementor-element-32c6ed5 elementor-widget elementor-widget-eael-testimonial" data-id="32c6ed5" data-element_type="widget" data-widget_type="eael-testimonial.default">
				<div class="elementor-widget-container">
			
	<div id="eael-testimonial-32c6ed5" class="eael-testimonial-item clearfix  default-style">

		
		
								<div class="eael-testimonial-content">
				<div class="eael-testimonial-text"><p>Performance is one of the primary reasons why this library was created.</p>
<p><a href="https://react-hook-form.com/faqs" target="_blank" rel="noopener">https://react-hook-form.com/faqs</a></p>
</div>			</div>
		
		
		
		
		
		<span class="eael-testimonial-quote"></span>
	</div>

			</div>
				</div>
				<div class="elementor-element elementor-element-1a3ea45 elementor-widget elementor-widget-text-editor" data-id="1a3ea45" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
							<p>What is <strong>Yup?</strong></p><p>Yup is a library used for creating schemas that help in parsing and validating values at runtime.</p>						</div>
				</div>
				<div class="elementor-element elementor-element-b732a55 elementor-widget elementor-widget-code-block-for-elementor" data-id="b732a55" data-element_type="widget" data-widget_type="code-block-for-elementor.default">
				<div class="elementor-widget-container">
			<pre class='line-numbers theme-okaidia' data-show-toolbar='yes'><code class='language-cil'>npm install react-hook-form
npm install @hookform/resolvers yup</code></pre>		</div>
				</div>
				<div class="elementor-element elementor-element-dc49f8b elementor-widget elementor-widget-text-editor" data-id="dc49f8b" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
							<p>I have created simple form using <a href="https://getbootstrap.com/" target="_blank" rel="noopener">bootstrap</a>.</p>						</div>
				</div>
				<div class="elementor-element elementor-element-e29a66b elementor-widget elementor-widget-image" data-id="e29a66b" data-element_type="widget" data-widget_type="image.default">
				<div class="elementor-widget-container">
			<style>/*! elementor - v3.21.0 - 08-05-2024 */
.elementor-widget-image{text-align:center}.elementor-widget-image a{display:inline-block}.elementor-widget-image a img[src$=".svg"]{width:48px}.elementor-widget-image img{vertical-align:middle;display:inline-block}</style>										<img loading="lazy" decoding="async" loading="lazy" width="1024" height="442" src="https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_22h12_07-1024x442.png" class="attachment-large size-large wp-image-8795" alt="simple form" srcset="https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_22h12_07-1024x442.png 1024w, https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_22h12_07-300x129.png 300w, https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_22h12_07-768x331.png 768w, https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_22h12_07.png 1370w" sizes="auto, (max-width: 1024px) 100vw, 1024px" />													</div>
				</div>
				<div class="elementor-element elementor-element-9420c74 elementor-widget elementor-widget-text-editor" data-id="9420c74" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
							<p>In a standard case, we would create a <strong>useState </strong>or <strong>useRef</strong> for each field. It would result in a massive amount of code. However, it&#8217;s a good approach when you are new to <strong>React</strong> and still learning. By implementing it yourself, you will learn how <strong>React</strong> works and how to solve basic problems that surprise beginners. Okay, let&#8217;s get back to the main thread.</p>						</div>
				</div>
				<div class="elementor-element elementor-element-79259ee elementor-widget elementor-widget-text-editor" data-id="79259ee" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
							<p>I have created <strong>React</strong> with typescript, so let&#8217;s create a type that defines our form.</p>						</div>
				</div>
				<div class="elementor-element elementor-element-195a85a elementor-widget elementor-widget-code-block-for-elementor" data-id="195a85a" data-element_type="widget" data-widget_type="code-block-for-elementor.default">
				<div class="elementor-widget-container">
			<pre class='line-numbers theme-okaidia' data-show-toolbar='yes'><code class='language-typescript'>type FormData = {
    firstName: string;
    lastName: string;
    age: number;
    email: string;
    password: string;
}</code></pre>		</div>
				</div>
				<div class="elementor-element elementor-element-d4f8828 elementor-widget elementor-widget-text-editor" data-id="d4f8828" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
							<p>Then is good time to define our validation schema for this model.</p>						</div>
				</div>
				<div class="elementor-element elementor-element-57c22fc elementor-widget elementor-widget-code-block-for-elementor" data-id="57c22fc" data-element_type="widget" data-widget_type="code-block-for-elementor.default">
				<div class="elementor-widget-container">
			<pre class='line-numbers theme-okaidia' data-show-toolbar='yes'><code class='language-typescript'>const schema: ObjectSchema&lt;FormData&gt; = yup
    .object({
        firstName: yup.string().required(&quot;FirstName cannot be empty&quot;),
        lastName: yup.string().required(&quot;LastName cannot be empty&quot;),
        age: yup.number().positive(&quot;Age must be positive number&quot;).integer().required(&quot;Age cannot be empty&quot;).typeError(&quot;Age must be a number&quot;),
        email: yup.string().required(&quot;Email cannot be empty&quot;).matches(RegexConsts.email, &quot;Email must contain @ and . characters&quot;),
        password: yup.string().required(&quot;Password cannot be empty&quot;),
    });</code></pre>		</div>
				</div>
				<div class="elementor-element elementor-element-18bbc8a elementor-widget elementor-widget-text-editor" data-id="18bbc8a" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
							<p>Yup provides many validation methods. I won&#8217;t waste your time, so here I will only touch on the <code>matches</code> method.</p>						</div>
				</div>
				<div class="elementor-element elementor-element-6ed6ceb elementor-widget elementor-widget-text-editor" data-id="6ed6ceb" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
							<p><code>Matches</code> method allows us to define regex pattern to validate this field. The second parameters means failed validation message.</p>						</div>
				</div>
				<div class="elementor-element elementor-element-97bdf71 elementor-widget elementor-widget-code-block-for-elementor" data-id="97bdf71" data-element_type="widget" data-widget_type="code-block-for-elementor.default">
				<div class="elementor-widget-container">
			<pre class='line-numbers theme-okaidia' data-show-toolbar='yes'><code class='language-javascript'>export const RegexConsts = {
    email: /.*[@].*[.][a-zA-Z]{2,}/
}</code></pre>		</div>
				</div>
				<div class="elementor-element elementor-element-5874aaa elementor-widget elementor-widget-text-editor" data-id="5874aaa" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
							<p>React Hook Form validates forms based on configuration, in this case after submitting the form.</p>						</div>
				</div>
				<div class="elementor-element elementor-element-82204ba elementor-widget elementor-widget-image" data-id="82204ba" data-element_type="widget" data-widget_type="image.default">
				<div class="elementor-widget-container">
													<img loading="lazy" decoding="async" loading="lazy" width="1024" height="505" src="https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_23h00_49-1024x505.png" class="attachment-large size-large wp-image-8808" alt="validation failed" srcset="https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_23h00_49-1024x505.png 1024w, https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_23h00_49-300x148.png 300w, https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_23h00_49-768x379.png 768w, https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_23h00_49.png 1339w" sizes="auto, (max-width: 1024px) 100vw, 1024px" />													</div>
				</div>
				<div class="elementor-element elementor-element-df05710 elementor-widget elementor-widget-text-editor" data-id="df05710" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
							<p>Custom validation for age</p>						</div>
				</div>
				<div class="elementor-element elementor-element-e972c28 elementor-widget elementor-widget-code-block-for-elementor" data-id="e972c28" data-element_type="widget" data-widget_type="code-block-for-elementor.default">
				<div class="elementor-widget-container">
			<pre class='line-numbers theme-okaidia' data-show-toolbar='yes'><code class='language-typescript'>yup.number().positive(&quot;Age must be positive number&quot;).integer().required(&quot;Age cannot be empty&quot;).typeError(&quot;Age must be a number&quot;)</code></pre>		</div>
				</div>
				<div class="elementor-element elementor-element-307ad31 elementor-widget elementor-widget-image" data-id="307ad31" data-element_type="widget" data-widget_type="image.default">
				<div class="elementor-widget-container">
													<img loading="lazy" decoding="async" loading="lazy" width="678" height="96" src="https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_23h04_30.png" class="attachment-large size-large wp-image-8809" alt="type validation" srcset="https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_23h04_30.png 678w, https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_23h04_30-300x42.png 300w" sizes="auto, (max-width: 678px) 100vw, 678px" />													</div>
				</div>
				<div class="elementor-element elementor-element-67898fe elementor-widget elementor-widget-image" data-id="67898fe" data-element_type="widget" data-widget_type="image.default">
				<div class="elementor-widget-container">
													<img loading="lazy" decoding="async" loading="lazy" width="658" height="93" src="https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_23h01_09.png" class="attachment-large size-large wp-image-8810" alt="positive error validation" srcset="https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_23h01_09.png 658w, https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_23h01_09-300x42.png 300w" sizes="auto, (max-width: 658px) 100vw, 658px" />													</div>
				</div>
				<div class="elementor-element elementor-element-bc9140d elementor-widget elementor-widget-text-editor" data-id="bc9140d" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
							<p>When validation is successfully completed, the submit method returns our form object with the filled fields.</p>						</div>
				</div>
				<div class="elementor-element elementor-element-f3c2de6 elementor-widget elementor-widget-image" data-id="f3c2de6" data-element_type="widget" data-widget_type="image.default">
				<div class="elementor-widget-container">
													<img loading="lazy" decoding="async" loading="lazy" width="1024" height="512" src="https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_23h01_30-1024x512.png" class="attachment-large size-large wp-image-8811" alt="validation success" srcset="https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_23h01_30-1024x512.png 1024w, https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_23h01_30-300x150.png 300w, https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_23h01_30-768x384.png 768w, https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_23h01_30-1536x767.png 1536w, https://pawelmajewski.com/wp-content/uploads/2024/06/2024-06-01_23h01_30-2048x1023.png 2048w" sizes="auto, (max-width: 1024px) 100vw, 1024px" />													</div>
				</div>
				<div class="elementor-element elementor-element-cda05ce elementor-widget elementor-widget-text-editor" data-id="cda05ce" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
							<p>The full file looks as presented below</p>						</div>
				</div>
				<div class="elementor-element elementor-element-6724d21 elementor-widget elementor-widget-code-block-for-elementor" data-id="6724d21" data-element_type="widget" data-widget_type="code-block-for-elementor.default">
				<div class="elementor-widget-container">
			<pre class='line-numbers theme-okaidia' data-show-toolbar='yes'><code class='language-typescript'>import { useForm } from &quot;react-hook-form&quot;
import { yupResolver } from &quot;@hookform/resolvers/yup&quot;
import * as yup from &quot;yup&quot;
import { RegexConsts } from &quot;../consts/regexConsts&quot;
import { ObjectSchema } from &quot;yup&quot;

type FormData = {
    firstName: string;
    lastName: string;
    age: number;
    email: string;
    password: string;
}

const schema: ObjectSchema&lt;FormData&gt; = yup
    .object({
        firstName: yup.string().required(&quot;FirstName cannot be empty&quot;),
        lastName: yup.string().required(&quot;LastName cannot be empty&quot;),
        age: yup.number().positive(&quot;Age must be positive number&quot;).integer().required(&quot;Age cannot be empty&quot;).typeError(&quot;Age must be a number&quot;),
        email: yup.string().required(&quot;Email cannot be empty&quot;).matches(RegexConsts.email, &quot;Email must contain @ and . characters&quot;),
        password: yup.string().required(&quot;Password cannot be empty&quot;),
    });


export default function RegisterForm() {
    const {
        register,
        handleSubmit,
        formState: { errors },
    } = useForm&lt;FormData&gt;({
        resolver: yupResolver(schema),
    })

    const onSubmit = (data: FormData) =&gt; console.log(data)

    return (
        &lt;&gt;
            &lt;form className=&quot;card p-2&quot; onSubmit={handleSubmit(onSubmit)}&gt;
                &lt;div className=&quot;row&quot;&gt;
                    &lt;div className=&quot;col-md-6 mb-6&quot;&gt;
                        &lt;div className=&quot;input-group&quot;&gt;
                            &lt;label className=&quot;w-100&quot;&gt;First name
                                &lt;input type=&quot;text&quot; className=&quot;form-control&quot; {...register(&quot;firstName&quot;)} /&gt;
                            &lt;/label&gt;
                            &lt;div className=&quot;invalid-feedback d-block&quot;&gt;
                                {errors.firstName?.message}
                            &lt;/div&gt;
                        &lt;/div&gt;
                        &lt;div className=&quot;input-group&quot;&gt;
                            &lt;label className=&quot;w-100&quot;&gt;Last name
                                &lt;input type=&quot;text&quot; className=&quot;form-control&quot; {...register(&quot;lastName&quot;)} /&gt;
                            &lt;/label&gt;
                            &lt;div className=&quot;invalid-feedback d-block&quot;&gt;
                                {errors.lastName?.message}
                            &lt;/div&gt;
                        &lt;/div&gt;
                        &lt;div className=&quot;input-group&quot;&gt;
                            &lt;label className=&quot;w-100&quot;&gt;Age
                                &lt;input type=&quot;text&quot; className=&quot;form-control&quot; {...register(&quot;age&quot;)} /&gt;
                            &lt;/label&gt;
                            &lt;div className=&quot;invalid-feedback d-block&quot;&gt;
                                {errors.age?.message}
                            &lt;/div&gt;
                        &lt;/div&gt;
                        &lt;div className=&quot;input-group&quot;&gt;
                            &lt;label className=&quot;w-100&quot;&gt;Email
                                &lt;input type=&quot;text&quot; className=&quot;form-control&quot; {...register(&quot;email&quot;)} /&gt;
                            &lt;/label&gt;
                            &lt;div className=&quot;invalid-feedback d-block&quot;&gt;
                                {errors.email?.message}
                            &lt;/div&gt;
                        &lt;/div&gt;
                        &lt;div className=&quot;input-group&quot;&gt;
                            &lt;label className=&quot;w-100&quot;&gt;Password
                                &lt;input type=&quot;password&quot; className=&quot;form-control&quot; {...register(&quot;password&quot;)} /&gt;
                            &lt;/label&gt;
                            &lt;div className=&quot;invalid-feedback d-block&quot;&gt;
                                {errors.password?.message}
                            &lt;/div&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                    &lt;div className=&quot;col-md-6 mb-6&quot;&gt;
                        &lt;button type=&quot;submit&quot; className=&quot;btn btn-outline-success w-100&quot;&gt;Send&lt;/button&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/form&gt;
        &lt;/&gt;
    )
}
</code></pre>		</div>
				</div>
				<div class="elementor-element elementor-element-085804e elementor-widget elementor-widget-text-editor" data-id="085804e" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
							<p>I highly recommend using <strong>React Hook Form</strong> combined with <strong>Yup</strong> validation in your daily work with <strong>React</strong>.</p>						</div>
				</div>
					</div>
				</div>
				</div><p>The post <a href="https://pawelmajewski.com/simplify-your-react-forms-with-react-hook-form-and-yup/">Simplify Your React Forms with React Hook Form and Yup</a> first appeared on <a href="https://pawelmajewski.com">Software Developer's Tour</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://pawelmajewski.com/simplify-your-react-forms-with-react-hook-form-and-yup/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
