<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Georgey V B - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Georgey V B - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Tue, 23 Jun 2026 22:44:22 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/geobrodas/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Create a Feedback Form using the Google Sheets API ]]>
                </title>
                <description>
                    <![CDATA[ Google Sheets provide a simple way to create online forms and gather data from users. In this tutorial we will use Google Sheets and Next.js to build a simple form. We'll use Next.js as our front end, and we'll use Google Sheets as the back end to se... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/create-a-feedback-form-using-nextjs-and-google-sheets-api/</link>
                <guid isPermaLink="false">66ba608dbca875d7790d6aa1</guid>
                
                    <category>
                        <![CDATA[ api ]]>
                    </category>
                
                    <category>
                        <![CDATA[ forms ]]>
                    </category>
                
                    <category>
                        <![CDATA[ google sheets ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Georgey V B ]]>
                </dc:creator>
                <pubDate>Tue, 21 Sep 2021 17:44:05 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/09/Build-a-feedback-form-using-Google-Sheets-API.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Google Sheets provide a simple way to create online forms and gather data from users. In this tutorial we will use Google Sheets and Next.js to build a simple form.</p>
<p>We'll use Next.js as our front end, and we'll use Google Sheets as the back end to send the data we receive through a form. This way we can learn how to use Next.js and Google Sheets to build a simple form.</p>
<p>Here's what we'll cover in this tutorial: </p>
<ol>
<li>How to set up a new project in Google Cloud Console</li>
<li>How to connect the new project with a Google Sheet</li>
<li>How to create a front-end form in a Next.js application.</li>
<li>How to connect the form to the Google Sheet</li>
</ol>
<blockquote>
<p>To help you follow along, I have made a <a target="_blank" href="https://github.com/GeoBrodas/nextjs-form-using-google-sheets-api">GitHub repo</a>. If you ever get lost, have a look around.</p>
</blockquote>
<h1 id="heading-how-to-set-up-a-new-project-in-google-cloud-console">How to Set Up a New Project in Google Cloud Console</h1>
<p>To access the Google Sheets API, we first need to set up a new project on Google Cloud Console. Head over to the <a target="_blank" href="https://cloud.google.com">site</a>, go to the <a target="_blank" href="https://console.cloud.google.com">console</a> and make a new project. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/cloud-new.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>After the new project gets set up, go to <strong>APIs and Services</strong> and click on <strong>Enable APIs and Services.</strong></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/cloud-enable.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Search Google Sheets from the library and enable it.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/enable-sheets-api.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now, go to APIs and Services, then <strong>Credentials</strong> and click on <strong>New Credential.</strong> Make a new Service account.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/create-cred.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Give it a suitable name and fill in all the details. After generating the service account, copy the email ID somewhere. We'll need to add it to our Google Sheet afterward. We just created a Bot account to handle the various requests which will be sent from the front-end.</p>
<p>Next, click on the Service account in <strong>Credentials</strong> and them move to <strong>Keys.</strong> Click on <strong>Add Key.</strong> Make sure you set it to JSON format. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/google_key.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>A file will get downloaded on creating the new key. It contains all the environment variables that we need while connecting our front-end application to the Google Sheets.</p>
<h1 id="heading-how-to-connect-the-new-project-to-a-google-sheet">How to Connect the New Project to a Google Sheet</h1>
<p>Now let's connect the newly created project on Google Cloud Console with a Google Sheet. Head over to <a target="_blank" href="http://sheets.google.com/">Google Sheets</a> and create a new spreadsheet. </p>
<p>Before we move ahead, feel free to put in some raw data so that we have something to fetch while calling the requests in the next section. </p>
<p>With that done, click on Share and add the service account email we just created. Make sure you give it <strong>Editor access</strong> and un-check <strong>Notify people</strong>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/share.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now for the fun part. Let's go over to the code editor and create the front-end for our form.</p>
<h1 id="heading-how-to-create-the-front-end-form">How to Create the Front-end Form</h1>
<p>For building out the front end we'll use Next.js and use the API-routes feature to send a POST request to our Google Sheet.</p>
<p>Install Next using this command:</p>
<pre><code class="lang-bash">npx create-next-app
</code></pre>
<p>To build out the form and speed up the development process, we'll be using some third party packages. So go ahead and install the following:</p>
<pre><code class="lang-bash">npm i @chakra-ui/react @emotion/react@^11 @emotion/styled@^11 framer-motion@^4 react-hook-form
</code></pre>
<ul>
<li>Chakra-UI: an accessible framework that has personally helped me speed up the front-end design of most of my applications.</li>
<li>React-Hook-Form: helps you build efficient forms with client-side validation on the fly.</li>
</ul>
<p>In this tutorial, I'll be focusing more on executing the functionality of the form rather than building out the client-side validation. <a target="_blank" href="https://react-hook-form.com/get-started#Applyvalidation">Here</a> is a complete guide on adding client-side validation using <a target="_blank" href="https://react-hook-form.com/">React-Hook-Form</a>. Of course feel free to pay a visit to the <a target="_blank" href="https://chakra-ui.com/docs/getting-started">Chakra-UI</a> documentation as well.</p>
<p>After installing all the packages, open it using any code editor. In Next.js, each file you create in the <code>/pages</code> folder is an individual route. You can create a new one but here I'll be using the root file itself, that is <code>/pages/index.js</code>. </p>
<p>Clear out all the pre-generated lines of code. Now, let's make a basic structure for the Form. </p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { VStack, Text, Input } <span class="hljs-keyword">from</span> <span class="hljs-string">"@chakra-ui/react"</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Home</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">submitHandler</span> (<span class="hljs-params"></span>) </span>{
     <span class="hljs-comment">// POST request</span>
    }

    <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">VStack</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Text</span> <span class="hljs-attr">fontSize</span>=<span class="hljs-string">"2xl"</span> <span class="hljs-attr">fontWeight</span>=<span class="hljs-string">"bold"</span>&gt;</span>
            Your response matters!
          <span class="hljs-tag">&lt;/<span class="hljs-name">Text</span>&gt;</span>

          <span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">onSubmit</span>=<span class="hljs-string">{submitHandler}</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">Input</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Enter Name"</span> /&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">Button</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 class="hljs-tag">&lt;/<span class="hljs-name">VStack</span>&gt;</span></span>
    )
}
</code></pre>
<p>VStack wraps all the elements vertically. It's easy shorthand for <code>flex-direction: column</code>. The rest of the code should be pretty much self-explanatory</p>
<p>The beauty of Chakra-UI is that each of its components closely resembles actual HTML elements, greatly reducing the learning curve.</p>
<p>You may add some more input fields of your choice. Here's the end result:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/form.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now let's handle the form response when the user submits. For this we will use <code>react-hook-form</code>. </p>
<p>For getting the response from the form, we have to import the <code>useForm</code> hook, like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { useForm } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-hook-form'</span>;
</code></pre>
<p>From the hook, destructure the following:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> {
    register,
    handleSubmit
  } = useForm();
</code></pre>
<p>Wrap around the <code>submitHandler</code> we created earlier with <code>handleSubmit</code>:</p>
<pre><code class="lang-js">&lt;form onSubmit={handleSubmit(submitHandler)}&gt;
   {<span class="hljs-comment">/* Input fields here */</span>}               
&lt;/form&gt;
</code></pre>
<p>Now add <code>register</code> to all the input fields as follows:</p>
<pre><code class="lang-js">&lt;Input placeholder=<span class="hljs-string">"Enter your message"</span> {...register(<span class="hljs-string">'name'</span>) /&gt;
</code></pre>
<p>Now when the button is clicked, we should be able to see the entered data. For now simply log the data over to the console as follows:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">submitHandler</span> (<span class="hljs-params">data</span>) </span>{
    <span class="hljs-built_in">console</span>.log(data);
}
</code></pre>
<p>With that done, let's now create a new API-route for the POST request to be sent from the form.</p>
<h1 id="heading-how-to-connect-the-form-to-the-google-sheet">How to Connect the Form to the Google Sheet</h1>
<p>Create a new file in the <code>./pages/api/</code> route. Each file you make in this route is an api-route, which provides access to all Node.js features.</p>
<p>Go ahead and create a new file in the route, let's say <code>./pages/api/sheet.js</code>. Structure out a basic GET request to see if all works fine:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handler</span> (<span class="hljs-params">req, res</span>) </span>{
    res.json({<span class="hljs-attr">message</span>: <span class="hljs-string">"It works!"</span>});
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> handler;
</code></pre>
<p>To check if the API request is working at this point, go to <code>http://localhost:3000/api/sheet</code>.</p>
<p>With that done, let's first set up the POST request to be sent from the front-end using the native <code>Fetch</code> method.</p>
<pre><code class="lang-js"><span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">submitHandler</span> (<span class="hljs-params">data</span>) </span>{
    <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> fetch(<span class="hljs-string">"/api/sheet"</span>, {
            <span class="hljs-attr">method</span>: <span class="hljs-string">"POST"</span>,
            <span class="hljs-attr">body</span>: <span class="hljs-built_in">JSON</span>.stringify(data),
            <span class="hljs-attr">headers</span>: {
                <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'application/json'</span>,
              },
        })
}
</code></pre>
<p>Before doing anything else, we have to download another package:</p>
<pre><code class="lang-bash">npm install googleapis
</code></pre>
<p>On the API-route  (<code>/pages/api/sheet</code>), destructure the data we get from the front end.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {google} <span class="hljs-keyword">from</span> <span class="hljs-string">"googleapis"</span>

<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handler</span> (<span class="hljs-params">req, res</span>) </span>{
    <span class="hljs-keyword">if</span> (req.method === <span class="hljs-string">"POST"</span>){
            <span class="hljs-keyword">const</span> {name, message} = req.body;
        res.json({<span class="hljs-attr">message</span>: <span class="hljs-string">"It works!"</span>});
    }
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> handler;
</code></pre>
<p>Note: The API routes by default will listen for a GET request. So we have to explicitly check if the method is a POST request.</p>
<p>Before moving ahead, there's one last thing to set up, and that's the environment variables. Open the JSON file containing all the credentials while we created the new key.</p>
<p>Make a new file <code>.env.local</code> in the root. Go ahead and enter in the following variables.</p>
<pre><code class="lang-env">CLIENT_EMAIL=yourclientemail
CLIENT_ID=yourclientid
PRIVATE_KEY=yourprivatekey
SPREADSHEET_ID=yourspreadsheetid
</code></pre>
<p>With that done, we are pretty much finished setting up the API route to handle the request we'll be sending to our Google Sheet. First, let's create a authentication token:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> auth = <span class="hljs-keyword">new</span> google.auth.GoogleAuth({
    <span class="hljs-attr">credentials</span>: {
      <span class="hljs-attr">client_email</span>: process.env.CLIENT_EMAIL,
      <span class="hljs-attr">client_id</span>: process.env.CLIENT_ID,
      <span class="hljs-attr">private_key</span>: process.env.PRIVATE_KEY.replace(<span class="hljs-regexp">/\\n/g</span>, <span class="hljs-string">'\n'</span>),
    },
    <span class="hljs-attr">scopes</span>: [
      <span class="hljs-string">'https://www.googleapis.com/auth/drive'</span>,
      <span class="hljs-string">'https://www.googleapis.com/auth/drive.file'</span>,
      <span class="hljs-string">'https://www.googleapis.com/auth/spreadsheets'</span>,
    ],
  });
</code></pre>
<p>To gain access to the Google Sheet, our application needs to provide some scopes first – usually read and write access. </p>
<p>You can find more about scopes in the official <a target="_blank" href="https://developers.google.com/sheets/api/guides/authorizing">Google Sheets documentation</a>.</p>
<p>You might be wondering about the <code>replace</code> method I used in the third environment variable. This was due to a typical error I was encountering earlier. After browsing through Stack Overflow, I finally found the solution. Looks like the <code>PRIVATE_KEY</code> needs to be parsed properly by removing the slashes in the original key. This can be easily solved using the <code>replace</code> method.</p>
<p>You can find about this error in <a target="_blank" href="https://github.com/leerob/leerob.io/pull/342">this</a> Pull request I opened.</p>
<p>Next, pass in the auth token and specify the version of the API. The latest is v4.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> sheets = google.sheets({
    auth,
    <span class="hljs-attr">version</span>: <span class="hljs-string">'v4'</span>,
  });
</code></pre>
<p>Then we call the <code>spreadsheets.value.append</code> method to append the user entry into cells in the spreadsheet. </p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> sheets.spreadsheets.values.append({
      <span class="hljs-attr">spreadsheetId</span>: process.env.DATABASE_ID,
      <span class="hljs-attr">range</span>: <span class="hljs-string">'Sheet1!A2:C'</span>,
      <span class="hljs-attr">valueInputOption</span>: <span class="hljs-string">'USER_ENTERED'</span>,
      <span class="hljs-attr">requestBody</span>: {
        <span class="hljs-attr">values</span>: [[name, message]],
      },
    });
</code></pre>
<p>You can find the spreadsheet ID from the URL itself:</p>
<pre><code class="lang-url">https://docs.google.com/spreadsheets/d/{spreadsheetID}/edit#gid=0
</code></pre>
<p>The range determines which rows and columns the application must read from or write to. If you're confused with how to find the range, you can determine it using Google Sheets itself using the user-interface.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/select_range.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>The third property, <code>valueInputOption</code>, determines how the user entered value has to be parsed into the spreadsheet. </p>
<p>For example if the user has entered a number, then the spreadsheet will read it as a number as well. </p>
<p>The fourth property carries in the data to be appended into a particular cell. To append multiple values, you can put it all in an array, like in this case – name and message. </p>
<p>To end the API-route, at last send a response back to the front-end:</p>
<pre><code class="lang-js">res.status(<span class="hljs-number">201</span>).json({response, <span class="hljs-attr">result</span>: <span class="hljs-string">"Feedback posted to spreadsheet!"</span>})
</code></pre>
<p>If all goes well, you should be able to make a POST request and successfully add a new cell value into the spreadsheet.</p>
<h1 id="heading-conclusion">Conclusion</h1>
<p>Congratulations! You're ready to start collecting feedback. You could build out your own feedback form on your website. Or you could integrate with an existing service like Typeform. </p>
<p>But you want to keep your feedback form on your site, on your page. This is where integrating with the Google Sheets API comes in handy. </p>
<p>The Google Sheets API is pretty basic – it can read and write to spreadsheets. Moreover it's completely free, although there is a limitation on the API requests you can make within a particular time frame. </p>
<p>So Google Sheets API would be ideal for small-scale application and platforms with a smaller audience. If you ever have any questions, do ping me on <a target="_blank" href="https://twitter.com/BrodasGeo">Twitter</a>. </p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Chakra UI and React-Hook-Form –How to Build Beautiful Forms ]]>
                </title>
                <description>
                    <![CDATA[ In HTML, it’s the default behavior for forms to redirect to a new page whenever they're submitted. So in order to provide dynamic functionality, React uses a strategy called controlled components. If you have recently gone through a React course, you... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-use-react-hook-form-with-chakra-ui/</link>
                <guid isPermaLink="false">66ba6090bca875d7790d6aa3</guid>
                
                    <category>
                        <![CDATA[ Form validations ]]>
                    </category>
                
                    <category>
                        <![CDATA[ forms ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Georgey V B ]]>
                </dc:creator>
                <pubDate>Tue, 25 May 2021 20:26:38 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/05/Blog-7-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In HTML, it’s the default behavior for forms to redirect to a new page whenever they're submitted. So in order to provide dynamic functionality, React uses a strategy called <strong>controlled components</strong>.</p>
<p>If you have recently gone through a React course, you probably didn't like this part, because there are a lot of states to manage if you have multiple input fields.</p>
<p>Firstly, you track the state of the input field using the <code>onChange</code> property which calls the <code>useState()</code> function. The input fields are wrapped around a form element. </p>
<p>When the user submits the form, it triggers the <code>onClick</code> or <code>onSubmit</code> property to set the inputs either to an array containing values or objects, depending on the number of input fields.</p>
<p>Then comes validation, which checks whether the user has entered any input. If not, then return an error, prompting the user to enter a valid input. </p>
<p>The logic here is a whole lot of boilerplate. You might be bored just hearing about the process.</p>
<p>What if I tell you that a single library can achieve all of this?</p>
<p>Here is what I’ll cover in this article:</p>
<ol>
<li>What is React-Hook-Form?</li>
<li>How React-Hook-Form Affects Performance</li>
<li>How to Get User Data</li>
<li>How to Add Validation to Your Forms</li>
<li>How to Improve the Front End and User Experience Using Chakra-UI.</li>
</ol>
<h2 id="heading-what-is-react-hook-form">What is React-Hook-Form?</h2>
<p>React-Hook-Form is a flexible library that takes care of all your validation, state management, and user data – and it's all packed within a size of 25.3 kb (unpacked) and 9.1 kb GZip (changes with versions). </p>
<p>It's simple and straightforward to use, and you have to write minimal code.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/05/bundle-size.png" alt="Bundle size by bundlephobia.com" width="600" height="400" loading="lazy">
<em><a target="_blank" href="https://bundlephobia.com/result?p=react-hook-form@7.6.0">Image source</a></em></p>
<p>One of the features I’m impressed with is its performance. As mentioned in its <a target="_blank" href="https://react-hook-form.com/">official site</a>, React-Hook-Form:</p>
<p><em>“Minimizes the number of re-renders and faster mounting, striving to provide the best user experience.”</em></p>
<h2 id="heading-how-react-hook-form-affects-performance">How React-Hook-Form Affects Performance</h2>
<p>If you have been following the previous versions of the library, the documentation often referenced the <code>ref</code> property to handle state management and validation.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">”password”</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">”Password”</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">{register}</span> /&gt;</span>
</code></pre>
<p>In this way, React-Hook-Form adopts a method of <strong>uncontrolled</strong> input, rather than changing the state every single time. It isolates the selected component and avoids rendering the child components. </p>
<p>This significantly reduces the number of re-renders and boosts the overall performance of the application. (<a target="_blank" href="https://blog.logrocket.com/the-complete-guide-to-react-hook-form/#:~:text=React%20Hook%20Form%20adopts%20the,and%20it%20has%20zero%20dependencies.">Source</a>)</p>
<h2 id="heading-how-to-get-user-data-from-a-form">How to Get User Data from a Form</h2>
<p>I have made a Codesandbox for this particular tutorial, so make sure you refer to it in case you get lost.</p>
<p>Here's what you'll need to follow along:</p>
<ul>
<li><a target="_blank" href="https://cn7hq.csb.app/">App link</a></li>
<li><a target="_blank" href="https://codesandbox.io/s/funny-cartwright-cn7hq?file=/src/index.js">Source code</a></li>
</ul>
<p>And here’s what we will be building. It's a simple form with validation, alerts when a user submits, and errors that's built using Chakra UI.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/05/demo.png" alt="Demo of what we will be building" width="600" height="400" loading="lazy"></p>
<p>First, let's install the library:</p>
<pre><code class="lang-bash">npm install react-hook-form
</code></pre>
<p>Now import the <code>useForm</code> hook from the package:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { useForm } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-hook-form"</span>;
</code></pre>
<p>Destructure the following constants from the <code>useForm</code> hook:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { register, handleSubmit, <span class="hljs-attr">formState</span>: { errors } } = useForm();
</code></pre>
<p>Create a simple form element like this:</p>
<pre><code class="lang-js">&lt;form onSubmit={handleSubmit(onSubmit)}&gt;
    <span class="xml"><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">placeholder</span>=<span class="hljs-string">”First</span> <span class="hljs-attr">Name</span>” {<span class="hljs-attr">...register</span>(“<span class="hljs-attr">firstname</span>”)} /&gt;</span></span>
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">”submit”</span> /&gt;</span></span>
&lt;/form&gt;
</code></pre>
<p>On submit, the form will pass a function onto the handleSubmit function. We can define the <code>onSubmit</code> function like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> onSubmit = <span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(data);
</code></pre>
<p>Now if we test out our form element, when it's submitted the console returns the following:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/05/log-2.png" alt="Codesandbox console log preview" width="600" height="400" loading="lazy"></p>
<p>Looks like it returned an object with the property <code>firstname</code> and has successfully gotten ahold of the data in the input field.</p>
<p>Let’s now add a bunch more input fields: </p>
<pre><code class="lang-js">&lt;form onSubmit={handleSubmit(onSubmit)}&gt;
    <span class="xml"><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">placeholder</span>=<span class="hljs-string">”First</span> <span class="hljs-attr">Name</span>” {<span class="hljs-attr">...register</span>(“<span class="hljs-attr">firstname</span>”)} /&gt;</span></span>
    <span class="xml"><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">placeholder</span>=<span class="hljs-string">”Last</span> <span class="hljs-attr">Name</span>” {<span class="hljs-attr">...register</span>(“<span class="hljs-attr">lastname</span>”)} /&gt;</span></span>
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">”password”</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">”Your</span> <span class="hljs-attr">password</span>” {<span class="hljs-attr">...register</span>(“<span class="hljs-attr">password</span>”)} /&gt;</span></span>
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">”submit”</span> /&gt;</span></span>
&lt;/form&gt;
</code></pre>
<p>On logging the response, we get an object back:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/05/Surface-Pro-3---1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now, this data can be sent back to the database. But in this tutorial, let’s just display it back to the user, using <code>useState( )</code>.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> [data, setData] = useState();
<span class="hljs-keyword">const</span> onSubmit = <span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> {
   setData(data);
 };
</code></pre>
<p>Let’s make a new component called <code>Stats.js</code>. Here, we’ll make use of Chakra UI’s <code>Stat</code> component. </p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {
 HStack,
 Stack,
 Stat,
 StatHelpText,
 StatLabel
} <span class="hljs-keyword">from</span> <span class="hljs-string">"@chakra-ui/react"</span>;
<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Stats</span>(<span class="hljs-params">props</span>) </span>{
 <span class="hljs-keyword">return</span> (
   <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Stat</span> <span class="hljs-attr">mt</span>=<span class="hljs-string">{5}</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">Stack</span>
       <span class="hljs-attr">p</span>=<span class="hljs-string">{4}</span>
       <span class="hljs-attr">borderWidth</span>=<span class="hljs-string">"3px"</span>
       <span class="hljs-attr">borderRadius</span>=<span class="hljs-string">"md"</span>
       <span class="hljs-attr">direction</span>=<span class="hljs-string">"column"</span>
       <span class="hljs-attr">align</span>=<span class="hljs-string">"flex-start"</span>
     &gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">HStack</span>&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">StatLabel</span>&gt;</span>Name: {props.Firstname}<span class="hljs-tag">&lt;/<span class="hljs-name">StatLabel</span>&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">StatLabel</span>&gt;</span>{props.Lastname}<span class="hljs-tag">&lt;/<span class="hljs-name">StatLabel</span>&gt;</span>
       <span class="hljs-tag">&lt;/<span class="hljs-name">HStack</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">StatHelpText</span>&gt;</span>Password: {props.Password}<span class="hljs-tag">&lt;/<span class="hljs-name">StatHelpText</span>&gt;</span>
     <span class="hljs-tag">&lt;/<span class="hljs-name">Stack</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">Stat</span>&gt;</span></span>
 );
}
</code></pre>
<p>Now, import this component into your root file and pass the respective props.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> Stats <span class="hljs-keyword">from</span> <span class="hljs-string">"./Stats"</span>;
<span class="hljs-comment">// App.js</span>
{data &amp;&amp; (
       <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Stats</span>
         <span class="hljs-attr">Firstname</span>=<span class="hljs-string">{data.firstname}</span>
         <span class="hljs-attr">Lastname</span>=<span class="hljs-string">{data.lastname}</span>
         <span class="hljs-attr">Password</span>=<span class="hljs-string">{data.password}</span>
       /&gt;</span></span>
)}
</code></pre>
<p>The stats will only be displayed if the data is <code>true</code>.</p>
<p>The final result should look something like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/05/Surface-Pro-4---1.png" alt="Result displayed to front-end" width="600" height="400" loading="lazy"></p>
<h2 id="heading-how-to-add-validation-to-a-form">How to Add Validation to a Form</h2>
<p>Why do we need validation anyways? Let me take you through an example I went through during one of the hackathon projects I was working on. </p>
<p>I had built a Password manager, and I didn’t pay any attention to form validation. Users started entering empty passwords into the form. Eventually, there was a whole load of invalid data in the database.</p>
<p>Now if you visit any popular site, you might have noticed that it forces you to enter a strong password.</p>
<p>Let’s draw some conclusions from this case-study</p>
<ul>
<li>We want to get valid data</li>
<li>We want our user data to be protected by entering strong passwords</li>
<li>We want to keep the database clean by blocking invalid data</li>
</ul>
<p>We’ll try to achieve these objectives using client-side validation provided by React-Hook-Form. We will use the <code>register</code> method provided by the library. It allows you to register the data a user enters and apply validation rules to it.</p>
<p>Consider the example below: </p>
<pre><code class="lang-js">&lt;input
      type=<span class="hljs-string">"text"</span>
      placeholder=<span class="hljs-string">"First name"</span>
      {...register(<span class="hljs-string">"firstname"</span>, { <span class="hljs-attr">required</span>: <span class="hljs-literal">true</span> })}
/&gt;


By setting the rule required to <span class="hljs-string">`true`</span>, the user will be now forced to provide a string to pass the validation. We can further modify <span class="hljs-built_in">this</span> by returning a message back to the user when <span class="hljs-built_in">this</span> event occurs.

<span class="hljs-string">``</span><span class="hljs-string">`js
&lt;input
      type="text"
      placeholder="First name"
      {...register("firstname", { required: “Please enter your first name” })}
/&gt;
{errors.firstname &amp;&amp; &lt;p&gt;{errors.firstname.message}&lt;/p&gt;}</span>
</code></pre>
<p>If the object <code>errors</code> returns a true value, it will display the message to the user.</p>
<p>Let’s now force the user to provide a password with more than 8 characters. </p>
<pre><code class="lang-js">&lt;Input
      type=<span class="hljs-string">"password"</span>
      placeholder=<span class="hljs-string">"Password"</span>
      {...register(<span class="hljs-string">"password"</span>, {
        <span class="hljs-attr">required</span>: <span class="hljs-string">"Please enter Password"</span>,
        <span class="hljs-attr">minLength</span>: { <span class="hljs-attr">value</span>: <span class="hljs-number">8</span>, <span class="hljs-attr">message</span>: <span class="hljs-string">"Too short"</span> }
      })}
 /&gt;
</code></pre>
<p>By assigning the minLength property to 8, the user now will be compelled to enter an 8 character password.</p>
<p>Also, we can alert the user at the front-end by using the same method we used previously using <code>And operator</code>.</p>
<pre><code class="lang-js">{errors.password &amp;&amp; <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{errors.password.message}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>}
</code></pre>
<p>If you notice now, React-Hook-Form listens to the changes dynamically. So if the input is left empty, it displays an alert at the same time. If the password is too short, it updates the message dynamically. Beauty!</p>
<p>Now go ahead and make your own form. In the next section we’ll customise the form and send alerts to the user when an error pops up.</p>
<h2 id="heading-how-to-improve-the-front-end-and-user-experience-using-chakra-ui">How to Improve the Front End and User Experience Using Chakra-UI</h2>
<p>First of all let’s convert all the components we've used so far to Chakra UI components.</p>
<p>Instead of the normal <code>&lt;input /&gt;</code>, simply replace the existing one with the <code>Input</code> from Chakra UI. </p>
<pre><code class="lang-js">Import { Input } <span class="hljs-keyword">from</span> “@chakra-ui/react”
</code></pre>
<p>Now import the <code>VStack</code> and <code>Button</code> component the same way and wrap the whole form with <code>VStack</code>.</p>
<pre><code class="lang-js">&lt;VStack&gt;
         <span class="xml"><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">placeholder</span>=<span class="hljs-string">"First name"</span>
           {<span class="hljs-attr">...register</span>("<span class="hljs-attr">firstname</span>", {
             <span class="hljs-attr">required:</span> "<span class="hljs-attr">Please</span> <span class="hljs-attr">enter</span> <span class="hljs-attr">first</span> <span class="hljs-attr">name</span>",
             <span class="hljs-attr">minLength:</span> <span class="hljs-attr">3</span>,
             <span class="hljs-attr">maxLength:</span> <span class="hljs-attr">80</span>
           })}
         /&gt;</span></span>
         <span class="xml"><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">placeholder</span>=<span class="hljs-string">"Last name"</span>
           {<span class="hljs-attr">...register</span>("<span class="hljs-attr">lastname</span>", {
             <span class="hljs-attr">required:</span> "<span class="hljs-attr">Please</span> <span class="hljs-attr">enter</span> <span class="hljs-attr">Last</span> <span class="hljs-attr">name</span>",
             <span class="hljs-attr">minLength:</span> <span class="hljs-attr">3</span>,
             <span class="hljs-attr">maxLength:</span> <span class="hljs-attr">100</span>
           })}
         /&gt;</span></span>
         <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Input</span>
           <span class="hljs-attr">type</span>=<span class="hljs-string">"password"</span>
           <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Password"</span>
           {<span class="hljs-attr">...register</span>("<span class="hljs-attr">password</span>", {
             <span class="hljs-attr">required:</span> "<span class="hljs-attr">Please</span> <span class="hljs-attr">enter</span> <span class="hljs-attr">Password</span>",
             <span class="hljs-attr">minLength:</span> { <span class="hljs-attr">value:</span> <span class="hljs-attr">8</span>, <span class="hljs-attr">message:</span> "<span class="hljs-attr">Too</span> <span class="hljs-attr">short</span>" }
           })}
         /&gt;</span></span>
         <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Button</span>
           <span class="hljs-attr">borderRadius</span>=<span class="hljs-string">"md"</span>
           <span class="hljs-attr">bg</span>=<span class="hljs-string">"cyan.600"</span>
           <span class="hljs-attr">_hover</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">bg:</span> "<span class="hljs-attr">cyan.200</span>" }}
           <span class="hljs-attr">variant</span>=<span class="hljs-string">"ghost"</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>
&lt;/VStack&gt;
</code></pre>
<p>The props you see for the <code>Button</code> component are similar to the CSS properties, so migrating to Chakra UI won’t be a problem.</p>
<p>Now let’s give prompt alerts to the user when they enter any invalid data.</p>
<p>Make a new component <code>AlertPop.js</code>.</p>
<p>Import the following code: </p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { Alert, AlertIcon, AlertTitle } <span class="hljs-keyword">from</span> <span class="hljs-string">"@chakra-ui/react"</span>;
</code></pre>
<p>Let's define a prop for displaying the error message.</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">AlertPop</span> (<span class="hljs-params">props</span>) </span>{
 <span class="hljs-keyword">return</span> (
   <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Alert</span> <span class="hljs-attr">status</span>=<span class="hljs-string">"error"</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">AlertIcon</span> /&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">AlertTitle</span> <span class="hljs-attr">mr</span>=<span class="hljs-string">{2}</span>&gt;</span>{props.title}<span class="hljs-tag">&lt;/<span class="hljs-name">AlertTitle</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">Alert</span>&gt;</span></span>
 );
}
</code></pre>
<p>Now import this component into the root file, and under every input field type in the below logic:</p>
<pre><code class="lang-js">{errors.firstname &amp;&amp; <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">AlertPop</span> <span class="hljs-attr">title</span>=<span class="hljs-string">{errors.firstname.message}</span> /&gt;</span></span>}
{errors.lastname &amp;&amp; <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">AlertPop</span> <span class="hljs-attr">title</span>=<span class="hljs-string">{errors.lastname.message}</span> /&gt;</span></span>}
{errors.password &amp;&amp; <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">AlertPop</span> <span class="hljs-attr">title</span>=<span class="hljs-string">{errors.password.message}</span> /&gt;</span></span>}
</code></pre>
<p>Finally import <code>useToast</code> like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { useToast } <span class="hljs-keyword">from</span> <span class="hljs-string">"@chakra-ui/react"</span>
</code></pre>
<p>Using this component, we’ll be able to to display a pop-up message when the user successfully submits the form.</p>
<p>Here’s how we will perform this:</p>
<pre><code class="lang-js">Import { useToast } <span class="hljs-keyword">from</span> “@chakra-ui/react”

<span class="hljs-keyword">const</span> toast = useToast( )
<span class="hljs-keyword">const</span> onSubmit = <span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> {
   <span class="hljs-comment">//console.log(data);</span>
   toast({
     <span class="hljs-attr">title</span>: <span class="hljs-string">"Submitted!"</span>,
     <span class="hljs-attr">status</span>: <span class="hljs-string">"success"</span>,
     <span class="hljs-attr">duration</span>: <span class="hljs-number">3000</span>,
     <span class="hljs-attr">isClosable</span>: <span class="hljs-literal">true</span>
   });

   setData(data);
 };
</code></pre>
<p>The end result should look something like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/05/submit.png" alt="Toast component in action" width="600" height="400" loading="lazy"></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Web technology is advancing every day, at a very fast rate. It’s good to learn to use various libraries, but make sure you understand the basics first. </p>
<p>For example, you just can’t start learning ReactJS all of a sudden without understanding DOM manipulation. The basics are the foundation, else you won’t be able to understand the beauty of how the web works.</p>
<h2 id="heading-thanks-for-reading">Thanks for Reading!✨</h2>
<p>It’s good to see that you have read this far. If you did gain some insights from this article, feel free to share it with your community and co-workers. </p>
<p>I talk about web technologies and build projects, further documenting the development process for other developers to reference. If you need any guidance, make sure you hit me up on <a target="_blank" href="https://twitter.com/BrodasGeo">Twitter</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Why You Should Start Using Chakra UI ]]>
                </title>
                <description>
                    <![CDATA[ In this article, I'll talk about what ChakraUI is and why you should use it. Chakra UI is a component-based library. It's made up of basic building blocks that can help you build the front-end of your web application.  It is customizable and reusable... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/why-should-you-start-using-chakraui/</link>
                <guid isPermaLink="false">66ba6093e530197d3818e1a9</guid>
                
                    <category>
                        <![CDATA[ Accessibility ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Front-end Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Georgey V B ]]>
                </dc:creator>
                <pubDate>Tue, 11 May 2021 15:22:27 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/05/Why-should-you-start-using--5-.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this article, I'll talk about what ChakraUI is and why you should use it.</p>
<p>Chakra UI is a component-based library. It's made up of basic building blocks that can help you build the front-end of your web application. </p>
<p>It is customizable and reusable, and most importantly it supports ReactJs, along with some other libraries too. </p>
<p>Here's what we'll cover in this article:</p>
<ol>
<li>What is Chakra UI?</li>
<li>How to get Started and Install Chakra UI</li>
<li>Chakra UI Customization and Features</li>
<li>How Chakra UI affects your Lighthouse Score</li>
<li>How to use Dark Mode in Chakra UI</li>
<li>Conclusion</li>
</ol>
<h2 id="heading-what-is-chakra-ui">What is Chakra UI?</h2>
<p>Have you ever struggled with whether to focus more on the back-end or front-end of your project? Well believe me, both are equally important. </p>
<p>I started using Chakra UI because I wanted to focus on my back-end code more than being stuck on "How to center a div element?". </p>
<p>Chakra UI is extremely simple to use, especially when you are familiar with how to use ReactJs components.</p>
<h2 id="heading-how-to-get-started-and-install-chakra-ui">How to Get Started and Install Chakra UI</h2>
<p>Inside your respective directory, install ChakraUI using Yarn or NPM</p>
<p><code>yarn add @chakra-ui/react @emotion/react@^11 @emotion/styled@^11 framer-motion@^4</code></p>
<p><code>npm i @chakra-ui/react @emotion/react@^11 @emotion/styled@^11 framer-motion@^4</code></p>
<h4 id="heading-for-react">For React:</h4>
<p>For ChakraUI to get initialised you first need to add <code>&lt;ChakraProvider&gt;</code> in your <code>index.js</code> file.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>

<span class="hljs-comment">// 1. import `ChakraProvider` component</span>
<span class="hljs-keyword">import</span> { ChakraProvider } <span class="hljs-keyword">from</span> <span class="hljs-string">"@chakra-ui/react"</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params">{ Component }</span>) </span>{
 <span class="hljs-comment">// 2. Use at the root of your app</span>
 <span class="hljs-keyword">return</span> (
   <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ChakraProvider</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">Component</span> /&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">ChakraProvider</span>&gt;</span></span>
 )
}
</code></pre>
<h4 id="heading-for-nextjs">For Next.js</h4>
<p>Go to <code>pages/_app.js</code> and add the following lines of code:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { ChakraProvider } <span class="hljs-keyword">from</span> <span class="hljs-string">"@chakra-ui/react"</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyApp</span>(<span class="hljs-params">{ Component, pageProps }</span>) </span>{
 <span class="hljs-keyword">return</span> (
   <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ChakraProvider</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">Component</span> {<span class="hljs-attr">...pageProps</span>} /&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">ChakraProvider</span>&gt;</span></span>
 )
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> MyApp
</code></pre>
<p>(Source: <a target="_blank" href="https://chakra-ui.com/docs/getting-started">Chakra UI Docs</a>)</p>
<blockquote>
<p>You can refer the documentation to checkout ChakraUI’s support for other libraries: https://chakra-ui.com/docs/getting-started</p>
</blockquote>
<h2 id="heading-chakraui-customization-and-features">ChakraUI Customization and Features</h2>
<h3 id="heading-style-props">Style Props</h3>
<p>Chakra UI supports Reactjs, and every component is customizable using the Style props. They map to almost all necessary CSS properties that are available. </p>
<p>For example, for <code>margin-top</code> in CSS, you would write it as
<code>&lt;Text mt={8} &gt;</code>. This will set a top margin of <code>8px</code> on the selected element.</p>
<p>Chakra UI is inspired by TailwindCSS's color palette, so you can find all your favorite colors!</p>
<h3 id="heading-how-to-override-chakrauis-default-theme">How to override ChakraUI's default theme</h3>
<p>You can override Chakra UI's default theme and create your own theme with the colors of your choice. You can do this using Chakra UI’s CSS variables.</p>
<p>All you need to do is either make a new <code>theme.js</code> file or edit the existing <code>index.js</code> file on React or the <code>_app.js</code> file on Nextjs. </p>
<pre><code class="lang-js"><span class="hljs-comment">// 1. Import `extendTheme`</span>
<span class="hljs-keyword">import</span> { extendTheme } <span class="hljs-keyword">from</span> <span class="hljs-string">"@chakra-ui/react"</span>

<span class="hljs-comment">// 2. Call `extendTheme` and pass your custom values</span>
<span class="hljs-keyword">const</span> theme = extendTheme({
 <span class="hljs-attr">colors</span>: {
   <span class="hljs-attr">brand</span>: {
     <span class="hljs-number">100</span>: <span class="hljs-string">"#f7fafc"</span>,
     <span class="hljs-comment">// ...</span>
     <span class="hljs-number">900</span>: <span class="hljs-string">"#1a202c"</span>,
   },
 },
})

<span class="hljs-comment">// 4. Now you can use these colors in your components</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Usage</span>(<span class="hljs-params"></span>) </span>{
 <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Box</span> <span class="hljs-attr">bg</span>=<span class="hljs-string">"brand.100"</span>&gt;</span>Welcome<span class="hljs-tag">&lt;/<span class="hljs-name">Box</span>&gt;</span></span>
}
</code></pre>
<p>(Source: <a target="_blank" href="https://chakra-ui.com/docs/theming/customize-theme">Chakra Ui Docs</a>)</p>
<blockquote>
<p>For more information on overriding the ChakraUI default theme, visit the Chakra UI docs at https://chakra-ui.com/docs/theming/customize-theme</p>
</blockquote>
<h3 id="heading-responsive-styles">Responsive styles</h3>
<p>How about responsiveness? The biggest headache, for me at least. I don't enjoy this part, but with Chakra UI, it’s no longer a huge pain. </p>
<p>For example, consider the line of code below:</p>
<pre><code class="lang-js">&lt;Box m={[<span class="hljs-number">2</span>, <span class="hljs-number">3</span>]} /&gt;
</code></pre>
<p>(Source: <a target="_blank" href="https://chakra-ui.com/docs/features/style-props">Chakra UI Docs</a>)</p>
<p>So now by defining this value inside an array, the Box component will be <code>8px</code> on all viewports, and <code>16px</code> from the first breakpoint.</p>
<p>Now we can override this in many ways, one of which is easier to understand – using the object syntax.</p>
<pre><code class="lang-js">&lt;Text fontSize={{ <span class="hljs-attr">base</span>: <span class="hljs-string">"24px"</span>, <span class="hljs-attr">md</span>: <span class="hljs-string">"40px"</span>, <span class="hljs-attr">lg</span>: <span class="hljs-string">"56px"</span> }}&gt;
 This is responsive text
&lt;/Text&gt;
</code></pre>
<p>(Source: <a target="_blank" href="https://chakra-ui.com/docs/features/style-props">Chakra UI Docs</a>)</p>
<p>So now on smaller screens, the fontSize is <code>24px</code>, on medium size it's <code>40px</code>, and on larger screens it's <code>56px</code>.</p>
<p>You might have noticed that Chakra UI strictly follows the ReactJs syntax for defining inline styles by capitalizing the second word (that is <code>fontSize</code>) as in the CSS property <code>font-size</code> for its style props too.  </p>
<blockquote>
<p>For more information on Responsive styles visit the ChakraUI docs at
https://chakra-ui.com/docs/features/responsive-styles</p>
</blockquote>
<h3 id="heading-stack-component">Stack Component</h3>
<p>Another commonly-used feature which I used to dislike in CSS is the <code>flex</code> property. It's a bit confusing to grasp how the property works.</p>
<p>Well it’s my honor to say here – “Chakra UI to the rescue🚀!”</p>
<p>I'd like to introduce you to the Stack component.</p>
<p>Stack is a simple layout component which you can use to stack elements, vertically and horizontally. </p>
<p>So there is Stack, HStack (short hand for Horizontal Stack), and VStack (short hand for Vertical stack). You might have guessed it right by now, but HStack will stack the elements horizontally and VStack will do the same layout vertically – but most importantly with zero CSS.</p>
<blockquote>
<p>For more information on the Stack component, visit the Chakra UI Docs at
https://chakra-ui.com/docs/layout/stack.</p>
</blockquote>
<h2 id="heading-how-chakra-ui-affects-your-lighthouse-score">How Chakra UI affects your Lighthouse Score</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/05/score-2.JPG" alt="score-2" width="600" height="400" loading="lazy"></p>
<p>When you're finally ready to deploy a web application, you should first run it through Google Lighthouse. </p>
<p>Google Lighthouse is an automation tool that is in-built on your Chrome developer tools. It helps you run audits on your web applications, and determines a score based on their performance, accessibility, progressive web apps, SEO, and much more. </p>
<p>The word we want to look out for here is Accessibility.  </p>
<h3 id="heading-what-is-web-accessibility">What is Web Accessibility?</h3>
<p>As a developer, it’s our responsibility to make the web acessible for everyone, and Google is taking this matter very seriously. </p>
<p>When websites are properly designed, it helps everyone. Proper design means that, for example, screen readers should be able to properly read out the elements on your page to a user. These principles are reflected in the the Web Accessibility Initiative (WAI). </p>
<p>Good accessibility doesn't just benefit people with disabilities. It's also helpful to users with smartphones, smart TVs, screens of all sizes, older people who might not see their screens very well, users with color blindness, and people using slow internet connections.</p>
<p>For more information on WAI you can visit their official site at
https://www.w3.org/WAI/.</p>
<h3 id="heading-what-does-chakraui-have-to-do-with-accessibility">What does ChakraUI have to do with accessibility?</h3>
<p>ChakraUI follows all the standards laid down by WAI for all its components. So all you have to do is add the <code>Aria-label</code> property to the Chakra component. </p>
<p>This is just what Chakra does behind-the-scenes to help you during the development process. </p>
<h3 id="heading-but-why-do-we-have-to-follow-wai-anyways-what-if-we-dont">But why do we have to follow WAI anyways? What if we don’t?</h3>
<p>As I said, Google is takes accessibility very seriously. As a result, the search engine will rank your page based partly on your accessibility score. That’s why Lighthouse has dedicated audits for accessibility.</p>
<h2 id="heading-how-to-use-dark-mode-in-chakraui">How to use Dark Mode in ChakraUI</h2>
<p>Dark mode is ever more popular these days, and ChakraUI makes it easy to use.</p>
<p>Let's say that you're in a React project with the below <code>index.js</code> file:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">'react-dom'</span>
<span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/App'</span>
<span class="hljs-keyword">import</span> { ChakraProvider } <span class="hljs-keyword">from</span> <span class="hljs-string">'@chakra-ui/react'</span>

ReactDOM.render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ChakraProvider</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">ChakraProvider</span>&gt;</span></span>,
  <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>)
)
</code></pre>
<p>All you need to do to initialise Dark mode on your site is make a few changes in the <code>index.js</code> file and make a Button component for toggling dark mode.</p>
<p>Let’s initialise the hook for toggling between dark and light mode first.</p>
<p>Head over to the <code>index.js</code> file, and type in the following lines of code:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">'react-dom'</span>
<span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/App'</span>
<span class="hljs-keyword">import</span> { ChakraProvider, ColorModeScript } <span class="hljs-keyword">from</span> <span class="hljs-string">'@chakra-ui/react'</span>

ReactDOM.render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ChakraProvider</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">ColorModeScript</span> <span class="hljs-attr">initialColorMode</span>=<span class="hljs-string">"light"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">ChakraProvider</span>&gt;</span></span>,
  <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>)
)
</code></pre>
<p>The hook <code>ColorModeScript</code> will track the mode which is currently set on the site.</p>
<p>Set the prop <code>initialColorMode</code> to “light”, “dark”, or “system”.</p>
<p>Now head over to your <code>App.js</code> file.</p>
<p>Install react-icons using npm like this:
<code>npm i react-icons</code></p>
<p>Import two React icons:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { FaMoon, FaSun } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-icons/fa'</span>
</code></pre>
<p>Import the following ChakraUI component and <code>useColorMode</code> hook and initialise the hook.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {IconButton, useColorMode } <span class="hljs-keyword">from</span> <span class="hljs-string">'@chakra-ui/react

const { colorMode, toggleColorMode } = useColorMode()</span>
</code></pre>
<p>The hook is similar to the <code>useState</code> hook, except that <code>toggleColorMode</code> will set the theme of the site to dark or light mode globally, while <code>colorMode</code> will store the value "light" or "dark".</p>
<p>Now let's bring in the icon button and give the component the following props:  </p>
<pre><code class="lang-js">&lt;IconButton
    icon={colorMode === <span class="hljs-string">'light'</span> ? <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">FaSun</span> /&gt;</span></span> : <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">FaMoon</span> /&gt;</span></span>}
    isRound=<span class="hljs-string">"true"</span>
    size=<span class="hljs-string">"lg"</span>
    alignSelf=<span class="hljs-string">"flex-end"</span>
    onClick={toggleColorMode}
/&gt;
</code></pre>
<p>Now when you click the button the site should change to Dark mode, and toggle between the two icons.</p>
<p>How easy was that?</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Chakra UI has helped me boost my development process to another level. It's very flexible, the documentation is great, and there are a lot of pre-built templates to help you speed up the process.</p>
<p>Two of the templates I want to call out are <a target="_blank" href="https://choc-ui.tech/">Choc UI</a> and <a target="_blank" href="https://chakra-templates.dev/">Chakra-Templates</a>. </p>
<p>Chakra UI also has a very active community on <a target="_blank" href="https://discord.com/invite/dQHfcWF">Discord</a>.</p>
<h2 id="heading-thank-you-for-reading-so-far">Thank you for Reading so far 🎉</h2>
<p>If you did like this article, share it with your co-workers.
I tweet about my development journey as a Self-taught developer, so hit me up on <a target="_blank" href="https://twitter.com/BrodasGeo">Twitter</a>.</p>
<p>Until then, have an amazing week!</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
