<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Hrithik Prasad | Projects]]></title><description><![CDATA[Throughout college, I worked on several projects that required the use of my skill sets, and I learned a lot while doing so. such as full stack development, and so on.]]></description><link>https://blog.hrithik.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Apr 2026 21:19:39 GMT</lastBuildDate><atom:link href="https://blog.hrithik.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Forms in React: A Comprehensive Guide]]></title><description><![CDATA[Forms are an essential part of any web application. They allow users to input data, register for accounts, make purchases, and more. In this post, we'll take a look at how to work with forms in React.
Introduction to Forms in React
In React, a form i...]]></description><link>https://blog.hrithik.dev/forms-in-react-a-comprehensive-guide</link><guid isPermaLink="true">https://blog.hrithik.dev/forms-in-react-a-comprehensive-guide</guid><category><![CDATA[React, forms, form input, validation, submission]]></category><dc:creator><![CDATA[hrithik prasad]]></dc:creator><pubDate>Sun, 01 Jan 2023 15:50:25 GMT</pubDate><content:encoded><![CDATA[<p>Forms are an essential part of any web application. They allow users to input data, register for accounts, make purchases, and more. In this post, we'll take a look at how to work with forms in React.</p>
<h2 id="heading-introduction-to-forms-in-react"><strong>Introduction to Forms in React</strong></h2>
<p>In React, a form is a component that contains form elements. These can include input fields, buttons, and other interactive elements. Forms can be used to collect data from users, validate input, and send data to a server.</p>
<p>The basic structure of a form in React looks like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, { useState } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyForm</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [formData, setFormData] = useState({});

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleChange</span>(<span class="hljs-params">event</span>) </span>{
    <span class="hljs-comment">// update formData with the new value</span>
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleSubmit</span>(<span class="hljs-params">event</span>) </span>{
    event.preventDefault();
    <span class="hljs-comment">// send formData to a server or do something else with it</span>
  }

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">onSubmit</span>=<span class="hljs-string">{handleSubmit}</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">label</span>&gt;</span>
        Name:
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"name"</span> <span class="hljs-attr">onChange</span>=<span class="hljs-string">{handleChange}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span>&gt;</span>Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span></span>
  );
}
</code></pre>
<p>In this example, we use the <code>useState</code> hook to store the form data in the state. We also define <code>handleChange</code> and <code>handleSubmit</code> functions to handle updates to the form data and form submission, respectively.</p>
<h2 id="heading-handling-form-input-with-state"><strong>Handling Form Input with State</strong></h2>
<p>In the example above, we used the <code>useState</code> hook to store the form data in the state. This is a convenient way to keep track of the values of form elements as the user inputs them.</p>
<p>To bind a form element to a state, we can use the <code>onChange</code> event. This event is triggered whenever the value of an input field changes.</p>
<p>Here's an example of how to use <code>onChange</code> to update the state:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleChange</span>(<span class="hljs-params">event</span>) </span>{
  setFormData({
    ...formData,
    [event.target.name]: event.target.value
  });
}
</code></pre>
<p>In this code, we use the spread operator (<code>...</code>) to create a new object that contains all the properties of <code>formData</code>, and then we use the computed property name syntax (<code>[</code><a target="_blank" href="http://event.target.name"><code>event.target.name</code></a><code>]</code>) to add a new property to the object with the name of the input field and the value of the input field as the value.</p>
<h2 id="heading-validating-form-input"><strong>Validating Form Input</strong></h2>
<p>Input validation is an important part of any form. It helps ensure that the data entered by the user is in the correct format and meets certain requirements.</p>
<p>There are several ways to validate form input in React:</p>
<ul>
<li><p><strong>HTML validation</strong>: Many form elements, such as <code>input</code> and <code>textarea</code>, have built-in validation attributes that can be used to ensure that the data entered by the user meets certain criteria. For example, the <code>required</code> an attribute can be used to require that a field is filled out, and the <code>pattern</code> an attribute can be used to specify a regular expression that the input must match.</p>
</li>
<li><p><strong>Third-party libraries</strong>: There are many libraries available that provide additional validation functionality beyond what is available with HTML. Some popular options include Formik, Yup, and React-Validation.</p>
</li>
<li><p><strong>Custom validation</strong>: It is also possible</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Generate Pdf Reports/Invoices in MERN Stack App.]]></title><description><![CDATA[To make pdf files sure there are a lot of different ways one can achieve the problem statement Some of the popular ones are pdfmake, pdfkit, and more. but these kinds of libraries don't have that much style control(that is what I believe and ad-hoc d...]]></description><link>https://blog.hrithik.dev/generate-pdf-reportsinvoices-in-mern-stack-app</link><guid isPermaLink="true">https://blog.hrithik.dev/generate-pdf-reportsinvoices-in-mern-stack-app</guid><category><![CDATA[College Project]]></category><category><![CDATA[pdf download]]></category><category><![CDATA[#react-pdf]]></category><category><![CDATA[pdf]]></category><category><![CDATA[React]]></category><dc:creator><![CDATA[hrithik prasad]]></dc:creator><pubDate>Thu, 29 Sep 2022 12:59:03 GMT</pubDate><content:encoded><![CDATA[<p>To make pdf files sure there are a lot of different ways one can achieve the problem statement Some of the popular ones are pdfmake, pdfkit, and more. but these kinds of libraries don't have that much style control(that is what I believe and ad-hoc design on them is a nightmare.) and I prefer the new way I found we could use some templating engine(ejs, pug, etc…) for this demo I used pug.js. and with that, I have a compiler to compile it down to plain HTML.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1664456058533/NEWO5ZZCw.png" alt="image.png" class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1664456070053/6UI6-LgWC.png" alt="image.png" class="image--center mx-auto" /></p>
<p>This is a sample pug template and for data, you can pass it when compiling, and to compile it and render you can make a route in express backend and return the response as "res.render("view",{data});"</p>
<p>Note: Remember to put this pug file in views and in the root file of your server you have declared the templating engine.</p>
<p>Now to get the view you can go to the express endpoint route you've declared and see if the view is perfect or not. and to test it in a robust environment you can also install the pug-cli compiler and watch the file while testing and designing this was a major feature I had. I simply used the following command. "pug - obj views/data.json views/index.pug -w" while the flags described are (double dash ) - obj for the data that needs to be passed along. and second, being -w as in watch for changes.</p>
<p>Now as we have our template complete and we can move to the front end and in it, I used a library called react-to-print and react-iframe to render the pug view.</p>
<p>for implementation, I used a form for data to send to the backend and process the view, and when I received the response than frontend open up a modal with two elements in it 1. button to trigger print and 2. the iframe with the URL of the view preview previously made in express to render the view. (you can have this view dynamic by receiving some IDs from the backend when you submit your form data). mine looks like this</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1664456127302/FtW5gZxtc.png" alt="image.png" class="image--center mx-auto" /></p>
<p>and for button onclick you can call the hook mentioned in the image and pass on the data required(see docs what can you pass and how can you manipulate). and if everything works fine then viola! you will get a prompt to the print dialog.</p>
]]></content:encoded></item></channel></rss>