<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Amarachi Nwokocha</title>
    <description>The latest articles on Forem by Amarachi Nwokocha (@amarachi_nwokocha_d823853).</description>
    <link>https://forem.com/amarachi_nwokocha_d823853</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2654842%2F93cec153-9c49-4fee-93a0-8db279bda60f.png</url>
      <title>Forem: Amarachi Nwokocha</title>
      <link>https://forem.com/amarachi_nwokocha_d823853</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/amarachi_nwokocha_d823853"/>
    <language>en</language>
    <item>
      <title>CSS styling and specificity</title>
      <dc:creator>Amarachi Nwokocha</dc:creator>
      <pubDate>Mon, 17 Mar 2025 13:13:51 +0000</pubDate>
      <link>https://forem.com/amarachi_nwokocha_d823853/css-styling-and-specificity-4ohi</link>
      <guid>https://forem.com/amarachi_nwokocha_d823853/css-styling-and-specificity-4ohi</guid>
      <description>&lt;p&gt;For a long time, CSS has been responsible for making web pages look beautiful, and interactive, and in some cases helped with basic functionalities.&lt;/p&gt;

&lt;p&gt;To do this, it must interact with the elements in the HTML (Hyper Text Markup Language) document.&lt;/p&gt;

&lt;p&gt;This guide, is for anyone trying to style HTML with vanilla CSS in any form. Be it in a standard HTML document, JSX, creating web components, or writing in PHP or XML, the rules still apply though the processes might be slightly different.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding CSS to an HTML Document
&lt;/h2&gt;

&lt;p&gt;There are several ways to add CSS to an HTML document in this article, we will cover three which are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Inline CSS&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;External CSS&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Internal CSS&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Inline CSS
&lt;/h3&gt;

&lt;p&gt;This method of adding CSS to HTML involves using the style attribute to directly embed CSS style rules into the element’s opening tag.&lt;/p&gt;

&lt;p&gt;(for details regarding html elements and how to use them, see my &lt;a href="https://medium.com/@nwokichaamarachi/html-tags-and-how-to-use-them-c12a0ad01c9" rel="noopener noreferrer"&gt;previous article&lt;/a&gt;)it will allow the element to look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb87gmu1751vijp8ir8lc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb87gmu1751vijp8ir8lc.png" alt=" " width="800" height="213"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While this method is convenient for quick styling, its repetitive nature makes it hard to maintain. It also affects the speed of an application as it increases the size of the html file thereby taking longer to load.&lt;/p&gt;

&lt;h3&gt;
  
  
  Internal CSS
&lt;/h3&gt;

&lt;p&gt;This method of styling adds the style rules to the head tag of the html document using the &lt;strong&gt;style&lt;/strong&gt; tag.&lt;/p&gt;

&lt;p&gt;While it’s great for styling single HTML documents, its single-page scope puts it at a disadvantage for styling multiple HTML documents.&lt;/p&gt;

&lt;p&gt;Also, mixing the structure of the html with CSS styles within the same document can make the HTML file harder to read and maintain.&lt;/p&gt;

&lt;p&gt;An example of the internal styling syntax can be found below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5odwz59m7c4923oshyid.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5odwz59m7c4923oshyid.png" alt=" " width="800" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  External CSS
&lt;/h3&gt;

&lt;p&gt;This is written in a separate file with a .css extension and is linked to an HTML document using the  tag in the head element.&lt;/p&gt;

&lt;p&gt;This is the best practice for adding CSS to HTML files for a few reasons some are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Separation of style information from HTML structure&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reusability of code as these external files can be linked to different HTML files&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reducing load times because browsers can cache external CSS files, so the CSS does not need to be re-downloaded.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This means of styling ensures uniform styling across multiple pages, making the website look cohesive and professional.&lt;br&gt;
Using this method of styling is very easy you just have to:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Create a CSS file with the .css extension so your file can be named style.css or home.css&lt;/li&gt;
&lt;li&gt;Link this CSS using the link element in the head tag of the HTML file
As seen below:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F18pms8px2xkrzpm50fy9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F18pms8px2xkrzpm50fy9.png" alt=" " width="800" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add your style rules to your CSS file. And you’re all set to style your page.&lt;/p&gt;

&lt;p&gt;The methods used to style the HTML page can vary for different reasons and conditions. While the external CSS method is the best practice, different circumstances call for different methods of styling so choose the method that best fits your project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Specificity in CSS
&lt;/h2&gt;

&lt;p&gt;Specificity in CSS refers to a set of rules that determine which CSS styles are applied to an element when there are multiple conflicting rules.&lt;/p&gt;

&lt;p&gt;In a nutshell, It is a way to calculate the weight of different CSS selectors. The more specific a selector is, the higher its specificity and the more likely its styles will be applied.&lt;/p&gt;

&lt;h3&gt;
  
  
  Specificity calculation.
&lt;/h3&gt;

&lt;p&gt;Specificity is determined by four major categories of selectors they are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Inline CSS:&lt;/strong&gt; (styles added directly to an HTML element via the style attribute) has the highest specificity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IDs:&lt;/strong&gt; unique identifiers for elements that have a high specificity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Classes, Attributes, and Pseudo-classes:&lt;/strong&gt; These selectors have medium specificity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Element (Type) Selectors and Pseudo-elements:&lt;/strong&gt; These have the lowest specificity.
In CSS, specificity is calculated through a point-based system.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This system starts at 0, element selectors and pseudo-element selectors which include pseudo-class or attribute selectors get 1 point, 10 points are given to classes, ID selector gets 100 points and inline styling gets 1000 points which is why it is always given the highest priority in CSS.&lt;/p&gt;

&lt;h4&gt;
  
  
  What this means for your code
&lt;/h4&gt;

&lt;p&gt;Let’s say we have an HTML document with an &lt;strong&gt;h1&lt;/strong&gt; element say and we style it in our external stylesheet so we have something like this in the HTML:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F41cjsbtrhspgkdioyh8k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F41cjsbtrhspgkdioyh8k.png" alt=" " width="800" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The styles applied to the H1 elements will be the styles from the ID selector because, on the specificity point list, the ID has more points than the element selector.&lt;/p&gt;

&lt;p&gt;Now say we throw in some inline styles for the H1 in the HTML document directly modifying the H1 to be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbktlzzkjk9ppeche7h2o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbktlzzkjk9ppeche7h2o.png" alt=" " width="800" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The inline styles will automatically override the styles given by the ID because it has a higher specificity value than the ID.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why understanding specificity is important
&lt;/h3&gt;

&lt;p&gt;Styling with CSS allows for static ugly ducklings to transform into beautiful swans. The methods of styling are easy to understand and follow and though their usage may vary from project to project, external CSS remains the best practice.&lt;/p&gt;

&lt;p&gt;These methods however won’t be fully effective if you do not understand specificity and how to use it. Understanding specificity allows you to know why and how your styling will react to certain style rules.&lt;/p&gt;

&lt;p&gt;This helps minimise conflicting style rules, and redundant code and improves the overall performance of your web app. Happy coding!&lt;/p&gt;

</description>
      <category>css</category>
      <category>html</category>
      <category>specificity</category>
      <category>coding</category>
    </item>
    <item>
      <title>Level Up Your React Apps with Tailwind CSS: A Step-by-Step Guide</title>
      <dc:creator>Amarachi Nwokocha</dc:creator>
      <pubDate>Mon, 17 Mar 2025 12:55:22 +0000</pubDate>
      <link>https://forem.com/amarachi_nwokocha_d823853/level-up-your-react-apps-with-tailwind-css-a-step-by-step-guide-h27</link>
      <guid>https://forem.com/amarachi_nwokocha_d823853/level-up-your-react-apps-with-tailwind-css-a-step-by-step-guide-h27</guid>
      <description>&lt;p&gt;Have you ever had difficulties styling? Or had a rough day because one CSS rule decided to not do what it is meant to do? Is there an end to having multiple style files or cumbersome components because CSS is just being, well, CSS. We need not even mention the countless hours spent in writing and debugging media queries for responsive web design. These and many other problems are what web developers face everyday in order to build web applications that not only work but have beautiful interfaces. Introducing Tailwind CSS, the trusted CSS framework that brings style to life in the twinkle of an eye.&lt;/p&gt;

&lt;p&gt;Unlike most frameworks, Tailwind CSS does not have heavy classes. It is more of a shorthand method of writing CSS properties. You can think of it as an accessory added to an outfit to give it that extra pop. Now, what is tailwind CSS and how does it help writing readable and reusable code? And what are its benefits? Let’s take a deep dive to find out more.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; To fully grasp this guide, a good understanding of React and how to use it is required.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to Tailwind CSS
&lt;/h2&gt;

&lt;p&gt;Tailwind CSS is a utility-first CSS framework that provides a set of pre-built utility classes to help quickly build modern and responsive web interfaces. Unlike traditional CSS frameworks that come with pre-designed components, Tailwind focuses on providing low-level utility classes that enable the creation of custom designs without leaving your HTML or in our case JSX. A deeper understanding of Tailwind CSS can be found in my &lt;a href="https://dev.to/amarachi_nwokocha_d823853/tailwind-css-core-concepts-unveiling-the-power-of-utility-classes-4aem"&gt;previous article&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Tailwind CSS provides small modular classes that are designed to do one thing in CSS and do it well. These classes are added directly into the className attribute of an element and thereby applies the styles to that element. This is known as the utility first approach. This approach gives developers precise control over styling by allowing for quick adjustments without writing custom CSS. The class names are descriptive and directly represent the applied styles, making the code more readable and self-explanatory. It also makes it easy to combine and modify classes to create custom styles without leaving the HTML file.&lt;/p&gt;

&lt;p&gt;This guide is here to help you to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Break down the essentials of integrating Tailwind CSS with React.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Show you how to set it up without breaking a sweat.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Uncover the treasure trove of utility classes that Tailwind CSS offers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Share tips, tricks, and best practices for crafting dazzling interfaces.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Setting up a React project
&lt;/h3&gt;

&lt;p&gt;Setting up a ReactJS app can be done using different methods. Developers have various options ranging from using &lt;a href="https://create-react-app.dev/" rel="noopener noreferrer"&gt;create React App&lt;/a&gt; (CRA), using &lt;a href="https://vitejs.dev/guide/#scaffolding-your-first-vite-project" rel="noopener noreferrer"&gt;Vite&lt;/a&gt;, or any other method they are familiar with. Once the ReactJS app is integrated, installing Tailwind CSS as a dev dependency is pretty straight forward. The guide provided in the Tailwind CSS &lt;a href="https://tailwindcss.com/docs/guides/vite" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; is easy to read and quick to understand let’s break down the steps.&lt;/p&gt;

&lt;p&gt;Note: NodeJS must have already been installed on your machine for any of these steps to work as we will be using npm (Node Package Manager) to run these commands. For information on how to install node and what it is, visit the NodeJS &lt;a href="https://nodejs.org/en/download" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this guide we will be integrating our ReactJS app using vite.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; In the terminal of your choice, run the following command&lt;/p&gt;

&lt;p&gt;&lt;em&gt;npm create vite@latest my-project — — template react&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;this will create a ReactJs application named my-project. The name of the project can be changed based on your preference. The command can also be:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;npm create vite@latest movie-app — — template react&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The terminal will look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzg9wz76k47ezgujom8se.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzg9wz76k47ezgujom8se.png" alt="terminal view of tailwind installation." width="800" height="461"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Prompts to follow will be given such as cd my-project that changes the working directory to the my-project that we just initialized. Next, we run npm install to install the node modules folder and npm run dev if you like, to preview the newly installed app in the browser.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing Tailwind CSS
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Install tailwind css in the project to do this we run:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;npm install -D tailwindcss postcss autoprefixer&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;npx tailwindcss init -p&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;These will install Tailwind CSS and its dev dependencies then, generate your tailwind.config.js and postcss.config.js files.&lt;/p&gt;

&lt;p&gt;The terminal should look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlnieoe3ct2kcuuws5xv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlnieoe3ct2kcuuws5xv.png" alt=" " width="800" height="288"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; configuring template paths.&lt;/p&gt;

&lt;p&gt;To do this we open the project in the IDE and make some changes to the tailwind config file. Tailwind CSS provides a tailwind.config.js file in this file, configuration for the entire project goes in here. The first configuration we’d be doing is making changes to the content array. Upon initialization, the tailwind.config.js file looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faiwiroz9gpm25rn4tt4t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faiwiroz9gpm25rn4tt4t.png" alt=" " width="800" height="553"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The content section of your tailwind.config.js file is where you configure the paths to all of your HTML templates, JavaScript components, and any other source files that will contain Tailwind class names. As you can see, the content array is currently empty so add the following in the content array&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“./index.html”,&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“./src/*&lt;em&gt;/&lt;/em&gt;.{js,ts,jsx,tsx}”,&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The above code means that index.html and all the JS, TS, JSX, and TSX files in the src directory have access to the Tailwind CSS classes and can use them for styling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Add the &lt;a class="mentioned-user" href="https://dev.to/tailwind"&gt;@tailwind&lt;/a&gt; directives for each of Tailwind’s layers to your &lt;em&gt;./src/index.css&lt;/em&gt; file. These derivatives are:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/tailwind"&gt;@tailwind&lt;/a&gt; base;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/tailwind"&gt;@tailwind&lt;/a&gt; components;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/tailwind"&gt;@tailwind&lt;/a&gt; utilities;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's take a look at what each of these means&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/tailwind"&gt;@tailwind&lt;/a&gt; base;&lt;/em&gt; — This injects Tailwind’s base styles and any base styles registered by plugins.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/tailwind"&gt;@tailwind&lt;/a&gt; components;&lt;/em&gt; — This injects Tailwind’s component classes and any component classes registered by plugins.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/tailwind"&gt;@tailwind&lt;/a&gt; utilities;&lt;/em&gt; — This injects Tailwind’s utility classes and any utility classes registered by plugins.&lt;/p&gt;

&lt;p&gt;And that’s you’re all set! Integrating Tailwind CSS is as simple as can be!&lt;/p&gt;

&lt;h2&gt;
  
  
  Integrating Tailwind CSS in React Components
&lt;/h2&gt;

&lt;p&gt;As earlier stated, Tailwind CSS is a utility-first CSS framework that provides a set of pre-defined classes to help in the quick development of responsive and customizable user interface. These classes ae easy to use and very readable lets take a look at some of them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Colors:&lt;/strong&gt; Tailwind class for colors come in a pattern starting with what the color is for then moving to the color name and then to a number from 50–950 with the colors getting bolder as the number increases. For example, the class “text-blue-100” will add a light blue color to the text and “bg-gray-700” will give an element a background of a deep gray. This pattern works for all colors except black and white which have just one variation: it’s only text-white or text-black.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz3bmbe8ku5mb1yiv5vei.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz3bmbe8ku5mb1yiv5vei.png" alt=" " width="800" height="167"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Font size and styling:&lt;/strong&gt; Increase a font size of an element in tailwind is set much like the colors. By using the text and then adding the desired size which ranges from xs to 9xl with xs being extra small which is 14px or 0.875rem and 9xl being the biggest size the text can go which is about 8rem or 128px. Changing the font style is very similar to changing the font size. We start with the font and add your desired style. Example, font-bold, font-serif, font-semibold etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Padding and margin:&lt;/strong&gt; Tailwind CSS has a design system that would help you keep a consistent scale across your designs. All you have to know is the syntax for applying each utility. The utilities for adding padding are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· p —&lt;/strong&gt; which denotes padding across the whole element&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· pb —&lt;/strong&gt; which denotes padding-bottom&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· pl —&lt;/strong&gt; which denotes padding-left&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· pr —&lt;/strong&gt; which denotes padding-right&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· pt —&lt;/strong&gt; which denotes padding-top&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· py —&lt;/strong&gt; which denotes padding on the top and bottom&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· px —&lt;/strong&gt; which denotes padding on left and right.&lt;/p&gt;

&lt;p&gt;The utilities for margin the same just replace the p in padding with m for margin they are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· m —&lt;/strong&gt; which denotes margin on all sides of the element&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· mb —&lt;/strong&gt; which denotes margin-bottom&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· ml —&lt;/strong&gt; which denotes margin-left&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· mr —&lt;/strong&gt; denotes margin-right&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· mt —&lt;/strong&gt; denotes margin-top&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· mx —&lt;/strong&gt; denotes margin on the left and right&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· my —&lt;/strong&gt; denotes margin on the top and bottom.&lt;/p&gt;

&lt;p&gt;To apply them though, you’d have to use the appropriate numbers provided by Tailwind CSS. These numbers range from 0 to 96 with zero as 0px and 96 as 384px or 24rem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flex:&lt;/strong&gt; To use flex in Tailwind CSS you just need to add a class of flex and the then the desired flex direction. You can customize the layout by using different Flexbox utility classes provided by Tailwind CSS. For example, justify-between, items-center, flex-col, etc.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsplwis23pe1uosmuyzrd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsplwis23pe1uosmuyzrd.png" alt="flex in tailwind" width="800" height="236"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Grid:&lt;/strong&gt; Much like flexbox, you need to use the grid class and the desired grid direction. The layout can be customized by using the different grid utility classes provided by Tailwind CSS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs8j8o29dzy6lk8bzdord.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs8j8o29dzy6lk8bzdord.png" alt=" " width="738" height="214"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Responsive design:&lt;/strong&gt; In Tailwind CSS, responsive classes allow you to apply styles based on the screen size or viewport width. Tailwind uses a mobile-first approach, meaning styles are applied by default and can be overridden at larger screen sizes. Responsive classes are added using a specific syntax which are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· sm —&lt;/strong&gt; min-width 640px&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· md —&lt;/strong&gt; min-width 768px&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· lg —&lt;/strong&gt; min-width 1024px&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· xl–&lt;/strong&gt; min-width 1280px&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;· 2xl —&lt;/strong&gt; min-width 1536px&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpoefn1os9qes497t68bs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpoefn1os9qes497t68bs.png" alt="tailwind breakpoint" width="800" height="126"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code is an example of how Tailwind CSS responsive design works the background and font-size changes as the breakpoints changes the background will be slate-500 by default but on small screens it’ll be green-400, on medium screen size blue-600, on large screen size pink-500 and on extra-large screen size yellow-600. This concept can be used in making your web applications responsive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Customization and Extending Tailwind
&lt;/h2&gt;

&lt;p&gt;Though Tailwind CSS provides us with a wide range of colors and font styles, our project sometimes may require a shade of color now present in the pallet. In cases like this, Tailwind CSS provides us with the option of customizing our own colors. To do this, we go into the tailwind.config.js file and in the extend object we add our custom styles lets take colors for example.&lt;/p&gt;

&lt;p&gt;To add colors wed need to create a color object and within that object add the colors we need. It will look something like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyernrw1hcxs7ihh68c83.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyernrw1hcxs7ihh68c83.png" alt="integrating custom styles in tailwind config file." width="800" height="834"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we added the primary, secondary and accent colors. To use these colors in our project however, all we have to do is add the colors to the text or bg utility for text color and background respectively.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz320kjrswk2jo97br7iq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz320kjrswk2jo97br7iq.png" alt="using custom styles in a component" width="800" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These method can also be used for themes, breakpoint, typography, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Best practices and tips for using tailwind CSS.
&lt;/h3&gt;

&lt;p&gt;Now that we have a good understanding of Tailwind CSS and its amazing abilities, here are some tips I have found helpful while using Tailwind&lt;/p&gt;

&lt;p&gt;· Begin by using the default configuration and only add customizations as needed.&lt;/p&gt;

&lt;p&gt;· Avoid adding too many unnecessary classes upfront.&lt;/p&gt;

&lt;p&gt;· Use breakpoints (&lt;strong&gt;sm, md, lg, xl&lt;/strong&gt;) to adjust styles for different screen sizes.&lt;/p&gt;

&lt;p&gt;· Resist the temptation to mix Tailwind classes with inline styles. This can make your code harder to read and maintain.&lt;/p&gt;

&lt;p&gt;· Establish consistent naming conventions for custom utility classes to maintain clarity. Avoid generic names; choose names that clearly represent the purpose of the utility.&lt;/p&gt;

&lt;p&gt;· Stay updated with the Tailwind ecosystem, including new features, plugins, and best practices.&lt;/p&gt;

&lt;p&gt;· Use Tailwind extensions in your IDE like Tailwind CSS intellisense these help you get comfortable with the classes&lt;/p&gt;

&lt;p&gt;· Always consult the Tailwind CSS &lt;a href="https://tailwindcss.com/" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; and the Tailwind &lt;a href="https://tailwind.build/classes" rel="noopener noreferrer"&gt;CSS class list&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Tailwind and you.
&lt;/h2&gt;

&lt;p&gt;Tailwind CSS is a wonderful tool for building responsive website and web applications it comes with a wide range of utility classes that allow for rapid development by applying pre-defined classes directly in the components. It also provides a consistent and predictable set of utility classes. This consistency makes it easier for developers to maintain a unified design across the entire project.&lt;/p&gt;

&lt;p&gt;Tailwind also helps reduces the amount of CSS code you need to manage, leading to cleaner and more maintainable styles.it is easily configurable with the help of the its &lt;strong&gt;tailwind.config.js&lt;/strong&gt; file, which allows you to customize colors, spacing, breakpoints, and more to fit your project’s specific design requirements.&lt;/p&gt;

&lt;p&gt;So, what are you waiting for?! dive deep and explore this wonderful tool enjoy its features and create beautiful web interfaces. I can't wait to see what you build next!&lt;/p&gt;

</description>
      <category>tailwindcss</category>
      <category>frontend</category>
      <category>react</category>
      <category>programming</category>
    </item>
    <item>
      <title>Tailwind CSS Core Concepts: Unveiling the Power of Utility Classes</title>
      <dc:creator>Amarachi Nwokocha</dc:creator>
      <pubDate>Mon, 17 Mar 2025 12:10:15 +0000</pubDate>
      <link>https://forem.com/amarachi_nwokocha_d823853/tailwind-css-core-concepts-unveiling-the-power-of-utility-classes-4aem</link>
      <guid>https://forem.com/amarachi_nwokocha_d823853/tailwind-css-core-concepts-unveiling-the-power-of-utility-classes-4aem</guid>
      <description>&lt;p&gt;In the world of web development, striking a balance between rapid development and customizable, maintainable styles has always been a challenge. With the beauty of styling and the intricacy of functionality hanging in the balance, web developers often find themselves in a wild goose chase. Enter Tailwind CSS, a utility-first CSS framework that revolutionizes the way developers approach styling. In this article, we’ll delve into the essence of Tailwind CSS, exploring its core concepts, utility-first approach, configurations, responsive design, and overall philosophy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Tailwind CSS
&lt;/h2&gt;

&lt;p&gt;While traditional CSS frameworks often provide pre-built components with predefined styles, Tailwind takes a different route by emphasizing utility classes. It does this through a vast array of small, single-purpose classes that you can combine to create custom designs. For instance, instead of having a pre-made button class, you can craft a button using Tailwind’s utility classes for text color, padding, and border.&lt;/p&gt;

&lt;p&gt;Lets take a look at Tailwind CSS core concepts which are: Utility Classes, Configurations, and Responsive Design&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Utility-First Workflow&lt;/strong&gt; &lt;br&gt;
Tailwind CSS utility classes are small classes designed to target one specifit CSS properties and apply styling of the said properties. This promotes a more focused and efficient development process. Some examples of these classes are&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.bg-red-500:&lt;/strong&gt; Sets the background color to a specific shade of red.&lt;br&gt;
&lt;strong&gt;.p-4:&lt;/strong&gt; Provides padding of size 4.&lt;br&gt;
&lt;strong&gt;.text-lg&lt;/strong&gt;: Defines a larger text size.&lt;br&gt;
These classes allow developers to quickly prototype and iterate designs without writing custom CSS. enabling a more streamlined and efficient workflow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Configurations for Customization&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Tailwind CSS’s &lt;strong&gt;tailwind.config.js&lt;/strong&gt; file serves as a central hub for customization. Developers can modify default settings, define custom color palettes, adjust spacing, typography, breakpoints for responsive design, and more. This configuration empowers teams to maintain consistency across projects while tailoring styles to specific brand guidelines or design systems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Responsive Design Support&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Responsive design is a part of web development cannot be overlooked and Tailwind CSS provides a comprehensive set of responsive utility classes. These classes enable the creation of designs that adapt seamlessly across various screen sizes and devices. For instance:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.sm:text-lg:&lt;/strong&gt; Applies a larger text size on small screens.&lt;br&gt;
&lt;strong&gt;.md:grid-cols-2:&lt;/strong&gt; Sets a two-column grid layout on medium-sized screens.&lt;br&gt;
&lt;strong&gt;.lg:grid-cols-4:&lt;/strong&gt; sets a four column grid layout on large screen sizes.&lt;br&gt;
It is also important to note that Tailwind CSS follows a mobile first responsive design system. So using&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.text-sm sm:text-md md:text-lg lg:text-xl&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;will set a text to small font size as its default font size, medium font size for smaller screens, large font size for medium screen and extra large for larger screen sizes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Tailwind in your project
&lt;/h3&gt;

&lt;p&gt;Tailwind css Is easy to install and use there are various ways of using installing tailwind CSS in your project as found in the documentation &lt;a href="https://tailwindcss.com/docs/installation" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Tailwind Css provides the option of using a play CDN, or to run Tailwind CSS from scratch with the Tailwind CLI tool. Do note that to do this it is pertinent to have NodeJs installed in your machine.&lt;/p&gt;

&lt;p&gt;The full guide for installing Tailwind CSS in your project can be found &lt;a href="https://tailwindcss.com/docs/installation" rel="noopener noreferrer"&gt;here&lt;/a&gt; and to the guide for using the CDN can be found &lt;a href="https://tailwindcss.com/docs/installation/play-cdn" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you’re working with a framework like React or Vue, you might need additional setup to integrate Tailwind CSS effectively into your components. This can involve importing your CSS file, setting up webpack, or integrating Tailwind-specific plugins/extensions a guide to doing so can be found &lt;a href="https://tailwindcss.com/docs/guides/vite" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Embracing Tailwind CSS
&lt;/h3&gt;

&lt;p&gt;In summary, Tailwind’s utility-first approach contributes to the modularity and maintainability of code. Each utility class represents a specific style, making it easier to locate, modify, or remove styles as needed. Its configurable and customizable feature allows developers to define custom color palettes, adjust spacing, typography, breakpoints for responsive design, and more. This configuration empowers teams to maintain consistency across projects while tailoring styles to specific brand guidelines or design systems.&lt;/p&gt;

</description>
      <category>tailwindcss</category>
    </item>
    <item>
      <title>Understanding useContext in React</title>
      <dc:creator>Amarachi Nwokocha</dc:creator>
      <pubDate>Thu, 27 Feb 2025 15:37:27 +0000</pubDate>
      <link>https://forem.com/amarachi_nwokocha_d823853/understanding-usecontext-in-react-47ok</link>
      <guid>https://forem.com/amarachi_nwokocha_d823853/understanding-usecontext-in-react-47ok</guid>
      <description>&lt;p&gt;Passing down data in React components has been one of the major challenges faced by React developers since the introduction of functional components. Every component needs to use data, either wholly or in parts. While &lt;code&gt;useState&lt;/code&gt; has been our go-to for holding data locally, its limitations make it difficult to pass down data efficiently. In the past, we relied on props, leading to issues like prop drilling and cumbersome state management, making components error-prone and harder to maintain. This is where &lt;code&gt;useContext&lt;/code&gt; comes in.&lt;/p&gt;

&lt;p&gt;In this article, we will discuss:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is &lt;code&gt;useContext&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;How to set up Context in React&lt;/li&gt;
&lt;li&gt;How to use &lt;code&gt;useContext&lt;/code&gt; in components&lt;/li&gt;
&lt;li&gt;When to use &lt;code&gt;useContext&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Common mistakes and best practices&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On that note, let's dive into it!&lt;/p&gt;

&lt;h2&gt;
  
  
  What is &lt;code&gt;useContext&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;Simply put, &lt;code&gt;useContext&lt;/code&gt; is a way of sharing data across a React application without having to pass it manually through props. It works with &lt;code&gt;React.createContext&lt;/code&gt; to pass down data to components.&lt;/p&gt;

&lt;p&gt;Imagine we have a large farm, which undoubtedly leads to a large storehouse. This storehouse has a farmhand who keeps track of the tools and delivers them where needed. React Context is the storehouse that holds all the data or “tools,” and &lt;code&gt;useContext&lt;/code&gt; is our trusted farmhand that fetches these tools from storage and delivers them where needed.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useContext&lt;/code&gt; was introduced in React version 16.8. It leverages the Context API, which allows a user to store data in a global state. How does it do this? &lt;code&gt;React.createContext&lt;/code&gt; is used to create a Context object (which includes Provider and Consumer components), and &lt;code&gt;useContext&lt;/code&gt; is a hook that allows you to consume the context value from &lt;code&gt;React.createContext&lt;/code&gt;. It is important to note that the &lt;code&gt;useContext&lt;/code&gt; hook can only be used in functional components.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Set Up Context in a React Project
&lt;/h3&gt;

&lt;p&gt;Setting up Context in a React application is straightforward. Follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a context file.&lt;/li&gt;
&lt;li&gt;Create the context using the React Context API.&lt;/li&gt;
&lt;li&gt;Provide the context value.&lt;/li&gt;
&lt;li&gt;Wrap the app in the provider.&lt;/li&gt;
&lt;li&gt;Consume the context in the child component.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;See? Very easy! Now, let's put these steps into code.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 1: Create a Context File&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Some developers prefer to create a &lt;code&gt;context&lt;/code&gt; folder and store all context files inside it. This is especially useful when dealing with multiple contexts in a project.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 2: Create the Context using the React Context API&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;DataContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above, we are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Importing &lt;code&gt;useState&lt;/code&gt; (to locally hold the data).&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;createContext&lt;/code&gt; to create the context.&lt;/li&gt;
&lt;li&gt;Importing &lt;code&gt;useContext&lt;/code&gt; so we can send the data to the components.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 3: Provide the Context Value&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;In our project, the context value will be data fetched from an API and passed down to components. This ensures that all components have access to the fetched data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;DataProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DataContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;DataContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Custom hook to use the DataContext&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;DataContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above, we:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Created the &lt;code&gt;DataProvider&lt;/code&gt; component, which accepts &lt;code&gt;children&lt;/code&gt; as an argument.&lt;/li&gt;
&lt;li&gt;Initialized the &lt;code&gt;data&lt;/code&gt;, &lt;code&gt;loading&lt;/code&gt;, and &lt;code&gt;error&lt;/code&gt; states.&lt;/li&gt;
&lt;li&gt;Used &lt;code&gt;DataContext.Provider&lt;/code&gt; to wrap the &lt;code&gt;children&lt;/code&gt;, providing data to all components in the tree.&lt;/li&gt;
&lt;li&gt;Created a &lt;code&gt;useData&lt;/code&gt; hook to make accessing context data easier.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, let's fetch our data using &lt;code&gt;useEffect&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;axios&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://jsonplaceholder.typicode.com/users&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Step 4: Wrap the App in the Provider&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;To ensure our data is accessible to all components, we wrap our &lt;code&gt;App&lt;/code&gt; component with &lt;code&gt;DataProvider&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DataProvider&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Components&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;DataProvider&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Step 5: Consume the Context in the Child Component&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;DataContext&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../context/DataContext&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useContext&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;loading&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;DataContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, all values (&lt;code&gt;data&lt;/code&gt;, &lt;code&gt;error&lt;/code&gt;, &lt;code&gt;loading&lt;/code&gt;) can be used in any component!&lt;/p&gt;




&lt;h3&gt;
  
  
  When to Use &lt;code&gt;useContext&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;There are various scenarios where &lt;code&gt;useContext&lt;/code&gt; is useful:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Avoiding Prop Drilling&lt;/strong&gt; – When sharing data across multiple levels of components, &lt;code&gt;useContext&lt;/code&gt; can provide direct access to the data without passing props manually.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Managing Theme or UI Preferences&lt;/strong&gt; – For apps with cross-functional UI preferences such as light/dark mode, &lt;code&gt;useContext&lt;/code&gt; is a great way to manage state globally.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Centralizing API Data or Configurations&lt;/strong&gt; – When API data needs to be used across various components, &lt;code&gt;useContext&lt;/code&gt; is an efficient way to share this globally.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simplifying Component Composition&lt;/strong&gt; – When multiple components use the same logic or shared values (e.g., modals, notifications, form steps), &lt;code&gt;useContext&lt;/code&gt; provides a cleaner way to manage them.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Common Mistakes and Best Practices When Using &lt;code&gt;useContext&lt;/code&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Using Context for Everything&lt;/strong&gt; – Overusing &lt;code&gt;useContext&lt;/code&gt; when props would be a better choice. Instead, use &lt;code&gt;useContext&lt;/code&gt; for truly shared or global state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Using &lt;code&gt;useContext&lt;/code&gt; in a Component Not Wrapped in a Provider&lt;/strong&gt; – Ensure all components that consume context are wrapped in &lt;code&gt;&amp;lt;Context.Provider&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Using Multiple Contexts Inefficiently&lt;/strong&gt; – Avoid deeply nesting multiple contexts. Instead, use a custom provider component to combine them efficiently.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In this article, we covered:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The concept and importance of &lt;code&gt;useContext&lt;/code&gt; in React.&lt;/li&gt;
&lt;li&gt;How to set up and use &lt;code&gt;useContext&lt;/code&gt; effectively.&lt;/li&gt;
&lt;li&gt;Common pitfalls to avoid and best practices to follow.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;useContext&lt;/code&gt; is a powerful tool that simplifies state management in React applications. It eliminates unnecessary prop drilling and makes components cleaner and more maintainable. However, it's essential to use it wisely and only when necessary.&lt;/p&gt;

&lt;p&gt;To learn more, check out the official &lt;a href="https://react.dev/reference/react/useContext" rel="noopener noreferrer"&gt;React documentation on Context&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy coding! 🚀&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>frontend</category>
    </item>
    <item>
      <title>React Projects and State management</title>
      <dc:creator>Amarachi Nwokocha</dc:creator>
      <pubDate>Thu, 16 Jan 2025 11:01:09 +0000</pubDate>
      <link>https://forem.com/amarachi_nwokocha_d823853/react-projects-and-state-management-3m3b</link>
      <guid>https://forem.com/amarachi_nwokocha_d823853/react-projects-and-state-management-3m3b</guid>
      <description>&lt;p&gt;The core function of every application is its ability to manage, store, and manipulate data created within it or received from an external source.&lt;/p&gt;

&lt;p&gt;This data can be in the form of variables, objects, Booleans, etc. It can be of any data type supported by the language used. This data has to be stored, modified, and used in whatever way the application needs it.&lt;/p&gt;

&lt;p&gt;React, a JavaScript framework well known for its component-based structure works with all datatypes supported by JavaScript.&lt;/p&gt;

&lt;p&gt;One of the core features that makes React powerful and flexible is its ability to manage state within components.&lt;/p&gt;

&lt;p&gt;The useState hook, introduced in React 16.8, is a fundamental tool for managing state in functional components.&lt;/p&gt;

&lt;p&gt;This article will discuss :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What is useState hook?&lt;/li&gt;
&lt;li&gt;How is it used in an application?&lt;/li&gt;
&lt;li&gt;Best practices for using it&lt;/li&gt;
&lt;li&gt;Benefits of using useState hook in your application&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  What is useState hook?
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
State in react is an object that contains real-time data used by the application.&lt;/p&gt;

&lt;p&gt;It is a built-in React object that is used to contain data or information about the component. A component’s state can change over time; whenever it changes, the component re-renders.&lt;/p&gt;

&lt;p&gt;The useState hook is a powerful addition to React, introduced in version 16.8. It allows the management of state within functional components without the need for class-based components.&lt;/p&gt;

&lt;p&gt;It is important to note that one use of useState() can only be used to declare one state variable.&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;
&lt;h2&gt;
  
  
  How to use usestate hook in React syntax
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
To use the usestate hook, it has to be imported at the top level of your application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The usestate hook takes on an initial state and returns two values.&lt;/p&gt;

&lt;p&gt;The first value contains the state and the second value is a function that updates the state the value set to the state will be taken as its initial value.&lt;/p&gt;

&lt;p&gt;This is done by calling the usestate hook and passing the initial value you want to assign to that variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;apple&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setApple&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example, apple is the state setApple is the function that updates apple and 0 is apple’s initial value.&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Update state using React useState
&lt;/h2&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;p&gt;To update the value set by a state, the second value in the useState array will be called and the updated value will be passed into it’ll look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setApple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the value of apple will be equal to the value of whatever is put inside the bracket, in our case apple is set to 1.&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Best practices for usestate in react
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
While there are various tips on how best to use the usestate hook, adhering to the following tips helps you have clean reusable code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Declare the variable with “const”, it can’t be let or var&lt;/li&gt;
&lt;li&gt;Always have a default value while initializing even if it is zero this makes it readable and also avoids runtime issues.&lt;/li&gt;
&lt;li&gt;To prevent unnecessary re-renders, avoid unnecessary state&lt;/li&gt;
&lt;li&gt;Use multiple state variables for multiple pieces of state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Writing cleaner and more readable code that works is the goal of every developer, useState offers a clean way to handle data in an application to update the said data.&lt;/p&gt;

&lt;p&gt;This powerful hook can be used in various circumstances try it out in your next project and explore what it has to offer.&lt;/p&gt;

&lt;p&gt;Stuck on using a concept, feel free to reach out or drop a comment below I look forward to hearing from you.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
