<?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: Nwosa Tochukwu </title>
    <description>The latest articles on Forem by Nwosa Tochukwu  (@obere4u).</description>
    <link>https://forem.com/obere4u</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%2F687479%2F984eb016-a5ad-43bb-8370-811286870404.jpg</url>
      <title>Forem: Nwosa Tochukwu </title>
      <link>https://forem.com/obere4u</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/obere4u"/>
    <language>en</language>
    <item>
      <title>Why Nigeria's Tech Communities Deserve Better Infrastructure</title>
      <dc:creator>Nwosa Tochukwu </dc:creator>
      <pubDate>Thu, 26 Feb 2026 13:43:09 +0000</pubDate>
      <link>https://forem.com/obere4u/why-nigerias-tech-communities-deserve-better-infrastructure-39ml</link>
      <guid>https://forem.com/obere4u/why-nigerias-tech-communities-deserve-better-infrastructure-39ml</guid>
      <description>&lt;p&gt;Nigeria has one of the most exciting tech ecosystems on the continent. We have talented developers, ambitious founders, thriving communities, and events happening every single week across the country. But here is the problem nobody talks about — most of these events are invisible.&lt;/p&gt;

&lt;p&gt;Not because they are not happening. Because there is nowhere to find them.&lt;/p&gt;




&lt;h2&gt;
  
  
  My own experience
&lt;/h2&gt;

&lt;p&gt;In 2021, I was posted to Ekiti State for my NYSC. I started learning tech in 2022, excited about the journey ahead. But one thing kept frustrating me — I could not find tech events near me. No meetups, no workshops, no hackathons that I knew of. I assumed nothing was happening in Ekiti.&lt;/p&gt;

&lt;p&gt;When I relocated to Lagos in 2024, I expected it to be easier. And in some ways it was — events existed. But finding specifically tech events still took time, distractions, and luck. I would scroll through Eventbrite, get lost in unrelated events. I would check Twitter and miss announcements that came and went. I would find out about a great workshop three days after it happened.&lt;/p&gt;

&lt;p&gt;I was in Lagos — Nigeria's tech capital — and I was still missing events.&lt;/p&gt;

&lt;p&gt;That told me the problem was not about location. It was about infrastructure.&lt;/p&gt;




&lt;h2&gt;
  
  
  What we are missing
&lt;/h2&gt;

&lt;p&gt;Every thriving tech ecosystem has infrastructure that holds it together. Developer documentation. Community forums. Event platforms. Places where people can show up, connect, and grow together.&lt;/p&gt;

&lt;p&gt;Nigeria has the people. We have the talent. We have the energy. What we are missing is the connective tissue — the platforms that make it easy for communities to find each other.&lt;/p&gt;

&lt;p&gt;Right now, a developer in Enugu who wants to attend a Web3 workshop has to check multiple WhatsApp groups, scroll through Twitter, ask around, and hope someone mentions it in time. A student in Plateau trying to find their first hackathon has almost no way to discover what is happening near them.&lt;/p&gt;

&lt;p&gt;This is not a small inconvenience. Opportunities lost to poor visibility compound over time. The developer who never finds their community takes longer to grow. The organiser whose event reaches 20 people instead of 200 gets discouraged. The ecosystem moves slower than it should.&lt;/p&gt;




&lt;h2&gt;
  
  
  The gap nobody was filling
&lt;/h2&gt;

&lt;p&gt;What surprised me was that nobody had built a solution specifically for this. Global platforms like Eventbrite and Meetup exist, but Nigeria is an afterthought for them. They are not built around our 36 states. They do not understand that a developer in Kaduna needs a different discovery experience than someone in Lagos. They are expensive for grassroots organizers who are running community events for free.&lt;/p&gt;

&lt;p&gt;The Nigerian tech community deserved something built for it, not borrowed from somewhere else.&lt;/p&gt;




&lt;h2&gt;
  
  
  So I built TechLinkUp
&lt;/h2&gt;

&lt;p&gt;In 2025, I started building &lt;a href="https://techlinkup.xyz" rel="noopener noreferrer"&gt;TechLinkUp&lt;/a&gt; — Nigeria's first dedicated platform for tech event discovery. The idea is simple: one place where any Nigerian can find tech events in their state, in their category, for free.&lt;/p&gt;

&lt;p&gt;Today we have 153 events listed across categories like AI, Web3, fintech, cybersecurity, hackathons, women in tech, and more. We cover all 36 states. Listing an event is completely free for organizers. Finding one takes seconds for attendees.&lt;/p&gt;

&lt;p&gt;But I want to be honest — we are early. This is not a finished product. It is a foundation.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I really want to build
&lt;/h2&gt;

&lt;p&gt;The events platform is just the beginning. What excites me most is the data layer underneath it.&lt;/p&gt;

&lt;p&gt;Right now, nobody knows which states in Nigeria have the most active tech communities. Nobody knows which event formats — workshops, hackathons, conferences, meetups — drive the most engagement. Nobody knows which cities outside Lagos are quietly building something special but lack the visibility to attract the support they deserve.&lt;/p&gt;

&lt;p&gt;TechLinkUp will answer those questions. Not by guessing, but by building the infrastructure that makes that data exist in the first place.&lt;/p&gt;

&lt;p&gt;Imagine being able to show that Enugu has a growing Web3 community that nobody is paying attention to. Or that Ibadan developers are showing up consistently to AI events but have no dedicated space to grow. That data changes conversations. It directs investment, mentorship, and attention to where it is actually needed.&lt;/p&gt;




&lt;h2&gt;
  
  
  This is bigger than one platform
&lt;/h2&gt;

&lt;p&gt;I am a frontend developer with three years of experience. I built TechLinkUp alone, bootstrapped, because I felt the problem personally and could not wait for someone else to solve it.&lt;/p&gt;

&lt;p&gt;But this is not really about TechLinkUp. It is about what Nigeria's tech ecosystem becomes when we close the infrastructure gaps. When a developer in any of the 36 states can find their community as easily as someone in Lagos. When organizers in underrepresented cities get the visibility their work deserves. When we stop losing talent to isolation and missed opportunities.&lt;/p&gt;

&lt;p&gt;We have the people. We just need to connect them.&lt;/p&gt;




&lt;h2&gt;
  
  
  If you are building in Nigeria, I want to hear from you
&lt;/h2&gt;

&lt;p&gt;If you are an event organiser, list your event on TechLinkUp — it is free and takes five minutes.&lt;/p&gt;

&lt;p&gt;If you are a developer or tech enthusiast, check if there is something happening in your state. You might be surprised.&lt;/p&gt;

&lt;p&gt;And if you believe Nigeria's tech communities deserve better infrastructure, share this. The more people who know TechLinkUp exists, the faster we build the ecosystem we all want to be part of.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://techlinkup.xyz" rel="noopener noreferrer"&gt;techlinkup.xyz&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>techtalks</category>
      <category>web3</category>
      <category>webdev</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Hoisted and Confused? Let’s Decode JavaScript’s Scope Magic</title>
      <dc:creator>Nwosa Tochukwu </dc:creator>
      <pubDate>Mon, 12 May 2025 18:07:53 +0000</pubDate>
      <link>https://forem.com/obere4u/hoisted-and-confused-lets-decode-javascripts-scope-magic-358c</link>
      <guid>https://forem.com/obere4u/hoisted-and-confused-lets-decode-javascripts-scope-magic-358c</guid>
      <description>&lt;p&gt;JavaScript isn't broken — but if you've ever wondered why a &lt;strong&gt;variable returns &lt;code&gt;undefined&lt;/code&gt; or a function throws an error before it's even called&lt;/strong&gt;... you're not alone.&lt;/p&gt;

&lt;p&gt;Many developers hit weird bugs due to scope and hoisting without realizing what's really going on. I just dropped a post that breaks down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Why &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;const&lt;/code&gt; behave so differently&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How JavaScript really handles function and block scopes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What &lt;em&gt;hoisting&lt;/em&gt; actually does (and doesn’t)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How to write bug-free, predictable code&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you've ever thought “&lt;em&gt;JavaScript makes no sense&lt;/em&gt;”, this post is for you.&lt;/p&gt;

&lt;p&gt;👉 I wrote the full breakdown here:&lt;br&gt;
&lt;a href="https://www.linkedin.com/pulse/understanding-javascript-scope-hoisting-beginner-guide-nwosa-tochukwu-4sq2f/" rel="noopener noreferrer"&gt;Understanding JavaScript Scope and Hoisting: A Beginner to Intermediate Guide&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>frontend</category>
    </item>
    <item>
      <title>A Comparative Analysis of Alpine.js and ReactJS</title>
      <dc:creator>Nwosa Tochukwu </dc:creator>
      <pubDate>Fri, 28 Jun 2024 23:34:23 +0000</pubDate>
      <link>https://forem.com/obere4u/a-comparative-analysis-of-alpinejs-and-reactjs-3lk8</link>
      <guid>https://forem.com/obere4u/a-comparative-analysis-of-alpinejs-and-reactjs-3lk8</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Frontend development as we all know is consistently evolving with new technologies coming up to simplify the process of building interactive and dynamic web applications.&lt;/p&gt;

&lt;p&gt;In this article, we will be comparing two frontend technologies namely &lt;a href="https://alpinejs.dev/" rel="noopener noreferrer"&gt;Alpine.js&lt;/a&gt; and &lt;a href="https://react.dev/" rel="noopener noreferrer"&gt;Reactjs&lt;/a&gt;. We will get to know their unique features, strengths and use cases.&lt;/p&gt;

&lt;p&gt;As a participant in the &lt;a href="https://hng.tech/internship" rel="noopener noreferrer"&gt;HNG Internship&lt;/a&gt; program, where Reactjs is mostly used, I will also share my thoughts on Reactjs and my aspirations for the program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Alpine.js, what is it all about?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;a href="https://alpinejs.dev/" rel="noopener noreferrer"&gt;Alpine.js&lt;/a&gt; founded by &lt;a href="https://calebporzio.com/" rel="noopener noreferrer"&gt;Caleb Porzio&lt;/a&gt; in 2019 is a lightweight JavaScript framework designed to provide declarative behavior for HTML&lt;/em&gt;&lt;/strong&gt;. It is often seen as a simpler alternative to more complex frameworks like &lt;a href="https://react.dev/" rel="noopener noreferrer"&gt;Reactjs&lt;/a&gt; and &lt;a href="https://vuejs.org/" rel="noopener noreferrer"&gt;Vue.js&lt;/a&gt;, offering reactive and declarative syntax without the overhead of a full-blown framework.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key features of Alpine.js
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Simplicity and Lightweight:&lt;/strong&gt; Alpine.js is minimal, providing just enough reactivity to enhance behavior while the bundle size is small.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Declarative Syntax:&lt;/strong&gt; It allows developers to directly add interactivity with HTML making it easy to understand and maintain.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No Build Step Required:&lt;/strong&gt; Alpine.js can just be added through a CDN, removing the need for a build process and making it perfect for simple projects.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Recatjs, what is it all about?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://react.dev/" rel="noopener noreferrer"&gt;Reactjs&lt;/a&gt; founded by &lt;a href="https://www.meta.com/" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; in 2013 is a widely-used JavaScript library for building user interfaces (UI). It maintains component-based architecture and unidirectional data flow which have become industry standard&lt;/p&gt;

&lt;h2&gt;
  
  
  Key features of Reactjs
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Virtual DOM:&lt;/strong&gt; It uses virtual DOM (a mimic of the Real DOM) to optimize rendering, ensuring efficient updates and enhanced performance&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Component-Based Architecture:&lt;/strong&gt; Reactjs allows you to create reusable UI components. Promoting a maintainable and modular code base.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Rich Ecosystem:&lt;/strong&gt; Reactjs offers a comprehensive ecosystem for developers with its vast arrays of libraries and tools.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Comparative Analysis
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Performance
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Alpine.js:&lt;/strong&gt; With its minimalistic approach, Alpine.js adds very little overhead, resulting in fast initial load times and minimal performance impact.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ReactJS:&lt;/strong&gt; The virtual DOM in ReactJS enhances performance by minimizing direct DOM manipulations, but it may carry some runtime overhead compared to simpler libraries like Alpine.js.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Learning Curve
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Alpine.js:&lt;/strong&gt; Its simplicity and declarative nature make Alpine.js easy to learn, especially for developers familiar with HTML and JavaScript.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ReactJS:&lt;/strong&gt; Learning ReactJS requires more effort due to its extensive ecosystem and concepts like state management with hooks.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Ecosystem
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Alpine.js:&lt;/strong&gt; As a lightweight library, Alpine.js has a smaller ecosystem but can be easily integrated with other tools and frameworks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ReactJS:&lt;/strong&gt; ReactJS's mature and extensive ecosystem provides a wealth of libraries, tools, and community support, making it suitable for large-scale applications.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Use Cases
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Alpine.js:&lt;/strong&gt; Ideal for small to medium-sized projects where simplicity and speed are crucial. It excels at enhancing static HTML with interactivity, without a complicated setup.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ReactJS:&lt;/strong&gt; This is ideal for large-scale applications that demand robust state management and access to a rich ecosystem of supporting tools and libraries.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  My Journey with ReactJS in the HNG Internship
&lt;/h2&gt;

&lt;p&gt;With the start of my &lt;a href="https://hng.tech/premium" rel="noopener noreferrer"&gt;HNG Internship&lt;/a&gt; experience, I'm happy to go deeper into ReactJS. This internship presents a unique opening to work on actual projects, sharpen my skills, and collaborate within a vibrant community of developers.&lt;/p&gt;

&lt;p&gt;ReactJS's component-based architecture and extensive ecosystem align with my passion for building scalable and maintainable applications while maintaining ownership of my code structure. I am excited to utilize the power of ReactJS to create dynamic user interfaces and to gain hands-on experience in state management, routing, and performance optimization.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Both &lt;a href="https://alpinejs.dev/" rel="noopener noreferrer"&gt;Alpine.js&lt;/a&gt; and &lt;a href="https://react.dev/" rel="noopener noreferrer"&gt;ReactJS&lt;/a&gt; bring unique strengths to the frontend development landscape. Alpine.js's simplicity and lightweight nature make it a compelling choice for certain projects, while ReactJS's mature ecosystem and robust architecture are unparalleled for large-scale applications.&lt;/p&gt;

&lt;p&gt;If you're eager to delve into the ever-changing landscape of front-end development, I highly suggest exploring the &lt;a href="https://hng.tech/internship" rel="noopener noreferrer"&gt;HNG Internship&lt;/a&gt;. Though it may not be ideal for absolute novices, the program offers invaluable learning experiences and a chance to showcase your abilities.&lt;/p&gt;

&lt;p&gt;Developing a comprehensive understanding of the strengths and limitations of various front-end technologies will enable us to make informed decisions and build more effective web applications.&lt;/p&gt;

&lt;p&gt;Tochukwu Nwosa&lt;br&gt;
&lt;a href="mailto:obere4u@gmail.com"&gt;obere4u@gmail.com&lt;/a&gt;&lt;br&gt;
Frontend Developer&lt;br&gt;
&lt;a href="https://linkedin.com/in/nwosa-tochukwu" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>frontend</category>
      <category>javascript</category>
      <category>hng</category>
    </item>
    <item>
      <title>How to Perform Form Validation in React</title>
      <dc:creator>Nwosa Tochukwu </dc:creator>
      <pubDate>Wed, 22 Nov 2023 21:29:32 +0000</pubDate>
      <link>https://forem.com/obere4u/how-to-perform-form-validation-in-react-3kgi</link>
      <guid>https://forem.com/obere4u/how-to-perform-form-validation-in-react-3kgi</guid>
      <description>&lt;p&gt;This guide provides readers &lt;strong&gt;&lt;em&gt;with the knowledge and skills they need to implement form validation in React.&lt;/em&gt;&lt;/strong&gt; It covers all the essential aspects of form validation, from understanding the importance of validation to handling form submission.&lt;/p&gt;

&lt;p&gt;The guide begins by explaining the importance of form validation. It then shows how to set up a form component and implement validation logic. Next, it discusses how to display validation errors and handle form submissions. Finally, it covers real-time validation, validation libraries, customizing validation logic, testing form validation, and best practices for form validation in React.&lt;/p&gt;

&lt;p&gt;The guide is written in a step-by-step manner, making it easy to follow. It also includes code examples and screenshots to help readers understand the concepts.&lt;/p&gt;

&lt;p&gt;The ultimate goal of this guide is to help readers create robust and user-friendly forms with accurate data validation in React applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;I. Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Importance of form validation in web development.&lt;/li&gt;
&lt;li&gt;Benefits of using Form Validation in React.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;II. Setting up the Form Component&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new component for the form.&lt;/li&gt;
&lt;li&gt;Setup initial state and form fields.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;III. Implementing Form Validation Logic&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define validation rules and error messages.&lt;/li&gt;
&lt;li&gt;Handle form field changes and updates.&lt;/li&gt;
&lt;li&gt;Perform validation on form submission.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;IV. Displaying Validation Errors&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create an error state to store the validation form.&lt;/li&gt;
&lt;li&gt;Display an error message next to the invalid form field.&lt;/li&gt;
&lt;li&gt;Style the error message for better visuality.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;V. Handling Form Submission&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prevent Form submission if there are validation errors.&lt;/li&gt;
&lt;li&gt;Display a success message for a successful submission.&lt;/li&gt;
&lt;li&gt;Reset Form after submission&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;VI. Implementing Real-Time Validation&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Validate form fields as users interact or type with them.&lt;/li&gt;
&lt;li&gt;Provide feedback on field validity.
&lt;strong&gt;VII. Using Validation Libraries&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Explore popular form validation libraries  (e.g. Formik, Yup, etc.)
&lt;strong&gt;VIII. Customizing Validation Logic&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Implement custom validation functions for complex rules.&lt;/li&gt;
&lt;li&gt;Handle unique validation scenarios specific to your application.
&lt;strong&gt;IX. Testing Form Validation&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Write unit test for form validation logic.&lt;/li&gt;
&lt;li&gt;Test various scenarios, including valid and invalid.
&lt;strong&gt;X. Best Practices and Tips&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Keep validation logic separate from presentation components.&lt;/li&gt;
&lt;li&gt;Provide clear instructions and error messages to users.&lt;/li&gt;
&lt;li&gt;Consider accessibility and useability when designing forms
&lt;strong&gt;XI. Conclusion&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Recap the importance of form validation.&lt;/li&gt;
&lt;li&gt;Encouragement to implement form validation in your&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  I. Introduction
&lt;/h3&gt;

&lt;p&gt;Form validation is a critical part of web development. It ensures that user input is correct, complete, and meets specific requirements. &lt;br&gt;
This guide will walk you through the process of performing form validation in React, so you can create reliable and user-friendly forms for your applications.&lt;/p&gt;


&lt;h3&gt;
  
  
  II. Setting Up Form Component
&lt;/h3&gt;

&lt;p&gt;To start, we will create a new React component that will handle the form. This component will be the container for all form-related elements and logic. We will start by setting up the initial state to track form data and potential errors. We will then define the necessary form fields using React's controlled components, which will bind them to the state values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';

const MyForm = () =&amp;gt; {
  const [formData, setFormData] = useState({
    name: ' ',
    email: ' ',
    password: ' ',
  });

  // Event handlers for form field changes
  const handleInputChange = (e) =&amp;gt; {
    const { name, value } = e.target;
    setFormData((prevState) =&amp;gt; ({
      ...prevState,
      [name]: value,
    }));
  };

  // ...

  return (
    // JSX code for the form component
  );
};

export default MyForm; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  III. Implementing Form Validation Logic
&lt;/h3&gt;

&lt;p&gt;Now it is time to implement the logic for validating the form. First, define the validation rules for each form field and their corresponding error messages. For example, you might require a minimum length for a password or a valid email format for an email field.&lt;br&gt;
To handle changes to the form fields, set up event handlers that will capture user input. Within these event handlers, perform the necessary validation checks based on the defined rules and update the error state accordingly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MyForm = () =&amp;gt; {
  const [formData, setFormData] = useState({
    name: ' ',
    email: ' ',
    password: ' ',
  });

  const [formErrors, setFormErrors] = useState({
    name: ' ',
    email: ' ',
    password: ' ',
  });
  // Event handlers for form field changes
  const handleInputChange = (e) =&amp;gt; {
    const { name, value } = e.target;
    setFormData((prevState) =&amp;gt; ({
      ...prevState,
      [name]: value,
    }));
    // Perform validation checks and update the error state
    if (name === 'name' &amp;amp;&amp;amp; value.length &amp;lt; 3) {
      setFormErrors((prevState) =&amp;gt; ({
        ...prevState,
        name: 'Name must be at least 3 characters long.',
      }));
    } else if (name === 'email' &amp;amp;&amp;amp; !isValidEmail(value)) {
      setFormErrors((prevState) =&amp;gt; ({
        ...prevState,
        email: 'Please enter a valid email address.',
      }));
    } else if (name === 'password' &amp;amp;&amp;amp; value.length &amp;lt; 8) {
      setFormErrors((prevState) =&amp;gt; ({
        ...prevState,
        password: 'Password must be at least 8 characters long.',
      }));
    } else {
      setFormErrors((prevState) =&amp;gt; ({
        ...prevState,
        [name]: '', // Reset error message
      }));
    }
  };
   // other codes
}; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  IV. Displaying Validation Errors
&lt;/h3&gt;

&lt;p&gt;To provide users with clear feedback on validation errors, create an error state within the form component. When a form field fails validation, update the error state to store the corresponding error message. &lt;br&gt;
Then, display the error messages next to the respective form fields, making sure they are easily visible. Consider styling the error messages distinctively to draw attention and help users identify and correct their input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MyForm = () =&amp;gt; {
  // other codes

  return (
    &amp;lt;form&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;label htmlFor="name"&amp;gt;Name:&amp;lt;/label&amp;gt;
        &amp;lt;input
          type="text"
          id="name"
          name="name"
          value={formData.name}
          onChange={handleInputChange}
        /&amp;gt;
        {formErrors.name &amp;amp;&amp;amp; &amp;lt;p className="error"&amp;gt;{formErrors.name}&amp;lt;/p&amp;gt;}
      &amp;lt;/div&amp;gt;

      &amp;lt;div&amp;gt;
        &amp;lt;label htmlFor="email"&amp;gt;Email:&amp;lt;/label&amp;gt;
        &amp;lt;input
          type="email"
          id="email"
          name="email"
          value={formData.email}
          onChange={handleInputChange}
        /&amp;gt;
        {formErrors.email &amp;amp;&amp;amp; &amp;lt;p className="error"&amp;gt;{formErrors.email}&amp;lt;/p&amp;gt;}
      &amp;lt;/div&amp;gt;

      &amp;lt;div&amp;gt;
        &amp;lt;label htmlFor="password"&amp;gt;Password:&amp;lt;/label&amp;gt;
        &amp;lt;input
          type="password"
          id="password"
          name="password"
          value={formData.password}
          onChange={handleInputChange}
        /&amp;gt;
        {formErrors.password &amp;amp;&amp;amp; &amp;lt;p className="error"&amp;gt;{formErrors.password}&amp;lt;/p&amp;gt;}
      &amp;lt;/div&amp;gt;

      &amp;lt;button type="submit"&amp;gt;Submit&amp;lt;/button&amp;gt;
    &amp;lt;/form&amp;gt;
  );
}; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  V. Handling Form Submission
&lt;/h3&gt;

&lt;p&gt;When the user submits the form, prevent the submission if there are any validation errors present. Validate the form data using the defined rules and update the error state with any remaining validation failures.&lt;/p&gt;

&lt;p&gt;If the form passes validation, display a success message to indicate a successful submission. Additionally, consider resetting the form by clearing the fields to provide a fresh entry point for the user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MyForm = () =&amp;gt; {
  // other codes

  const handleSubmit = (e) =&amp;gt; {
    e.preventDefault();

    // Final validation check
    const isFormValid = Object.values(formErrors).every((error) =&amp;gt; error === ' ');

    if (isFormValid) {
      // Form submission logic
      console.log('Form submitted successfully!');
      // Reset the form
      setFormData({
        name: ' ',
        email: ' ',
        password: ' ',
      });
      setFormErrors({
        name: ' ',
        email: ' ',
        password: ' ',
      });
    } else {
      console.log('Form contains validation errors.');
    }
  };

  return (
    &amp;lt;form onSubmit={handleSubmit}&amp;gt;
      {/* Form fields and error messages */}
    &amp;lt;/form&amp;gt;
  );
}; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  VI. Implementing Real-Time Validation
&lt;/h3&gt;

&lt;p&gt;To enhance the user experience, you can implement real-time validation. This means checking the form fields for errors as the user types or interacts with them.&lt;/p&gt;

&lt;p&gt;You can use event handlers to capture user input in real-time and trigger the validation logic for the respective form fields. You can then provide instant feedback to users by indicating the validity of their input, such as highlighting the field or displaying an icon indicating success or failure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MyForm = () =&amp;gt; {
  // other code
  // Event handlers for form field changes
  const handleInputChange = (e) =&amp;gt; {
    const { name, value } = e.target;
    setFormData((prevState) =&amp;gt; ({
      ...prevState,
      [name]: value,
    }));
    // Perform validation checks and update error state
    if (name === 'name' &amp;amp;&amp;amp; value.length &amp;lt; 3) {
      setFormErrors((prevState) =&amp;gt; ({
        ...prevState,
        name: 'Name must be at least 3 characters long.',
      }));
    } else if (name === 'email' &amp;amp;&amp;amp; !isValidEmail(value)) {
      setFormErrors((prevState) =&amp;gt; ({
        ...prevState,
        email: 'Please enter a valid email address.',
      }));
    } else if (name === 'password' &amp;amp;&amp;amp; value.length &amp;lt; 8) {
      setFormErrors((prevState) =&amp;gt; ({
        ...prevState,
        password: 'Password must be at least 8 characters long.',
      }));
    } else {
      setFormErrors((prevState) =&amp;gt; ({
        ...prevState,
        [name]: ' ', // Reset error message
      }));
    }
  };
  // other codes
}; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  VII. Using Validation Libraries
&lt;/h3&gt;

&lt;p&gt;To streamline the form validation process, you can use existing validation libraries, such as &lt;a href="https://formik.org/docs/tutorial" rel="noopener noreferrer"&gt;Formik&lt;/a&gt; and &lt;a href="https://www.npmjs.com/package/yup" rel="noopener noreferrer"&gt;Yup&lt;/a&gt;. These libraries provide comprehensive form validation solutions and make it easy to integrate them into your React project.&lt;/p&gt;

&lt;p&gt;To get started, explore popular validation libraries and choose the one that best meets your needs. Install the library and integrate it into your React application by following the documentation. Then, use the library-specific methods and components to handle form validation seamlessly, reducing the amount of boilerplate code.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# Install Formik and Yup&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install formik yup&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Formik, Form, Field, ErrorMessage } from 'formik';
import * as Yup from 'yup';


const MyForm = () =&amp;gt; {
  // other codes  
  const initialValues = {
    name: '',
    email: '',
    password: '',
  };  
  const validationSchema = Yup.object({
    name: Yup.string().required('Name is required.'),
    email: Yup.string().email('Invalid email address.').required('Email is required.'),
    password: Yup.string().min(8, 'Password must be at least 8 characters long.').required('Password is required.'),
  });  
  const handleSubmit = (values, { resetForm }) =&amp;gt; {
    // Form submission logic
    console.log('Form submitted successfully!', values);
    resetForm();
  };  
  return (
    &amp;lt;Formik initialValues={initialValues} validationSchema={validationSchema} onSubmit={handleSubmit}&amp;gt;
      &amp;lt;Form&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;label htmlFor="name"&amp;gt;Name:&amp;lt;/label&amp;gt;
          &amp;lt;Field type="text" id="name" name="name" /&amp;gt;
          &amp;lt;ErrorMessage name="name" component="div" className="error" /&amp;gt;
        &amp;lt;/div&amp;gt;        
        &amp;lt;div&amp;gt;
          &amp;lt;label htmlFor="email"&amp;gt;Email:&amp;lt;/label&amp;gt;
          &amp;lt;Field type="email" id="email" name="email" /&amp;gt;
          &amp;lt;ErrorMessage name="email" component="div" className="error" /&amp;gt;
        &amp;lt;/div&amp;gt;        
        &amp;lt;div&amp;gt;
          &amp;lt;label htmlFor="password"&amp;gt;Password:&amp;lt;/label&amp;gt;
          &amp;lt;Field type="password" id="password" name="password" /&amp;gt;
          &amp;lt;ErrorMessage name="password" component="div" className="error" /&amp;gt;
        &amp;lt;/div&amp;gt;        
        &amp;lt;button type="submit"&amp;gt;Submit&amp;lt;/button&amp;gt;
      &amp;lt;/Form&amp;gt;
    &amp;lt;/Formik&amp;gt;
  );
}; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  VIII. Customizing Validation Logic
&lt;/h3&gt;

&lt;p&gt;Although many validation scenarios can be covered by built-in rules or validation libraries, you may encounter unique requirements specific to your application. In such cases, it is essential to be able to customize the validation logic.&lt;/p&gt;

&lt;p&gt;You can implement custom validation functions to handle complex rules that go beyond the built-in capabilities. These functions can address scenarios where multiple fields have dependencies or need to be validated together. You can adjust the validation logic to suit your specific use cases and integrate it into the existing validation flow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MyForm = () =&amp;gt; {
  // other codes  
  const isPasswordSecure = (password) =&amp;gt; {
    // Custom validation logic for password security
    return password.length &amp;gt;= 8 &amp;amp;&amp;amp; password.match(/[a-z]/) &amp;amp;&amp;amp; password.match(/[A-Z]/) &amp;amp;&amp;amp; password.match(/[0-9]/);
  };
  // other codes  
  const handleInputChange = (e) =&amp;gt; {
    // other codes
    // Validation check for password security
    if (name === 'password' &amp;amp;&amp;amp; !isPasswordSecure(value)) {
      setFormErrors((prevState) =&amp;gt; ({
        ...prevState,
        password: 'Password must be at least 8 characters long and contain at least one lowercase letter, one uppercase letter, and one digit.',
      }));
    }   
    //other codes
  };
  //other codes
}; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  IX. Testing Form Validation
&lt;/h3&gt;

&lt;p&gt;To ensure that your form validation is reliable and accurate, it is essential to write unit tests. These tests should cover a variety of scenarios, including valid and invalid input, to ensure that all edge cases are covered.&lt;/p&gt;

&lt;p&gt;You should verify that the error messages are displayed correctly and match the expected validation failures. You should also test the form's behavior when subjected to different validation conditions, ensuring that it handles errors appropriately and allows successful submissions when valid.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { render, screen, fireEvent } from '@testing-library/react';
import MyForm from './MyForm';

test('Form displays validation errors correctly', () =&amp;gt; {
  render(&amp;lt;MyForm /&amp;gt;);

  // Attempt to submit the form without entering any values
  fireEvent.click(screen.getByText('Submit'));

  // Verify that error messages are displayed
  expect(screen.getByText('Name is required.')).toBeInTheDocument();
  expect(screen.getByText('Email is required.')).toBeInTheDocument();
  expect(screen.getByText('Password is required.')).toBeInTheDocument();
});

// More tests for different scenarios and form validation rules
// other codes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  X Best Practices and Tips
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Keep the validation logic separate from the presentation components.&lt;/strong&gt; This will make your code more maintainable and modular, as you can easily reuse the validation logic in different forms.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Provide clear instructions and error messages.&lt;/strong&gt; This will help users understand what they need to do to correct their input. The error messages should be specific and helpful so that users can easily identify and fix the problem.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Consider accessibility and usability aspects.&lt;/strong&gt; This means using ARIA attributes for screen readers and ensuring that the form can be navigated using only the keyboard.&lt;br&gt;
By following these best practices, you can create effective form validation in your React applications and deliver a smooth and user-friendly experience.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  XI. Conclusion
&lt;/h3&gt;

&lt;p&gt;In this guide, we have explored the &lt;strong&gt;&lt;em&gt;process of performing form validation&lt;/em&gt;&lt;/strong&gt; in React. &lt;strong&gt;&lt;em&gt;By&lt;/em&gt;&lt;/strong&gt; setting up the &lt;strong&gt;&lt;em&gt;form component&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;implementing validation logic&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;displaying error messages&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;handling form submissions&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;leveraging real-time validation&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;utilizing validation libraries&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;customizing validation logic&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;testing form validation&lt;/em&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;em&gt;following best practices&lt;/em&gt;&lt;/strong&gt;, you can create robust and user-friendly forms in your React applications.&lt;/p&gt;

&lt;p&gt;With this knowledge, you are well-equipped to incorporate form validation in your React projects and enhance the overall user experience.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Build a Simple Shopping List App with JavaScript</title>
      <dc:creator>Nwosa Tochukwu </dc:creator>
      <pubDate>Sun, 11 Jun 2023 15:28:05 +0000</pubDate>
      <link>https://forem.com/obere4u/build-a-simple-shopping-list-app-with-javascript-38ln</link>
      <guid>https://forem.com/obere4u/build-a-simple-shopping-list-app-with-javascript-38ln</guid>
      <description>&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;In this tutorial, we'll walk through the process of building a simple shopping list app using JavaScript. &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%2F1j88w1wy5g0hpdxsquxw.gif" 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%2F1j88w1wy5g0hpdxsquxw.gif" alt="writing list" width="640" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This app will allow users to add items to a list, mark them as bought, and clear the entire list. It's a beginner-friendly project that will help you understand the basics of DOM manipulation and localStorage. So, let's get started!&lt;/p&gt;

&lt;p&gt;This is the live page&lt;br&gt;
&lt;a href="https://cart-companion.netlify.app/" rel="noopener noreferrer"&gt;https://cart-companion.netlify.app/&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Prerequisites:&lt;/strong&gt;&lt;br&gt;
Basic understanding of HTML, CSS, and JavaScript.&lt;br&gt;
A Text editor or integrated development environment (IDE) of your choice.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Setting Up the Project:&lt;/strong&gt;&lt;br&gt;
To begin, create a new directory for your project and set up the following files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;index.html - Copy the provided HTML code into this file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;style.css - Copy the provided CSS code into this file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;app.js - Copy the provided JavaScript code into this file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;site.webmanifest - Copy the provided site manifest code into this file.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Make sure to &lt;em&gt;link&lt;/em&gt; the &lt;code&gt;CSS&lt;/code&gt;, &lt;code&gt;JavaScript&lt;/code&gt;, and &lt;code&gt;Site Webmanifest&lt;/code&gt; files to the &lt;code&gt;HTML&lt;/code&gt; file using appropriate &lt;code&gt;&amp;lt;link&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tags.&lt;/p&gt;


&lt;h3&gt;
  
  
  Explaining the HTML Structure:
&lt;/h3&gt;

&lt;p&gt;Let's start by understanding the HTML structure of our shopping list app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8" /&amp;gt;
    &amp;lt;meta
      http-equiv="X-UA-Compatible"
      content="IE=edge"
    /&amp;gt;
    &amp;lt;meta
      name="viewport"
      content="width=device-width, initial-scale=1.0"
    /&amp;gt;
    &amp;lt;link
      rel="stylesheet"
      href="style.css"
    /&amp;gt;
    &amp;lt;link
      rel="preconnect"
      href="https://fonts.googleapis.com"
    /&amp;gt;
    &amp;lt;link
      rel="preconnect"
      href="https://fonts.gstatic.com"
      crossorigin
    /&amp;gt;
    &amp;lt;link
      href="https://fonts.googleapis.com/css2?family=Rubik:wght@300&amp;amp;display=swap"
      rel="stylesheet"
    /&amp;gt;
    &amp;lt;link
      rel="apple-touch-icon"
      sizes="180x180"
      href="/apple-touch-icon.png"
    /&amp;gt;
    &amp;lt;link
      rel="icon"
      type="image/png"
      sizes="32x32"
      href="/favicon-32x32.png"
    /&amp;gt;
    &amp;lt;link
      rel="icon"
      type="image/png"
      sizes="16x16"
      href="/favicon-16x16.png"
    /&amp;gt;
    &amp;lt;link
      rel="manifest"
      href="/site.webmanifest"
    /&amp;gt;
    &amp;lt;title&amp;gt;Cart Companion&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;div class="container"&amp;gt;
      &amp;lt;img
        src="assets/shopping-cart.png"
        alt="A cat inside a cart"
      /&amp;gt;
      &amp;lt;input
        type="text"
        id="input-field"
        placeholder="Item"
      /&amp;gt;
      &amp;lt;div id="action-button"&amp;gt;
        &amp;lt;button id="add-button"&amp;gt;Add to cart&amp;lt;/button&amp;gt;
        &amp;lt;button id="clear-button"&amp;gt;Clear cart&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;p id="empty-list"&amp;gt;No item here... yet&amp;lt;/p&amp;gt;

      &amp;lt;ul id="shopping-list"&amp;gt;&amp;lt;/ul&amp;gt;

      &amp;lt;div id="toast-container"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;

    &amp;lt;script
      src="app.js"
      type="module"
    &amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, we have the basic HTML structure with a container div where our app content will reside.&lt;/p&gt;




&lt;h3&gt;
  
  
  Styling the App with CSS
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;html,
body {
  margin: 0;
  padding: 0;
  font-family: "Rubik", sans-serif;
  background-color: #eef0f4;
  color: #432000;
  -webkit-user-select: none;
  user-select: none; /*so that users can't select anything*/
}

img {
  width: 150px;
  margin: 0 auto;
}

input {
  color: #432000;
  background-color: #dce1eb;
  border: 1px solid #000000;
  padding: 15px;
  border-radius: 8px;
  font-size: 20px;
  text-align: center;
  font-family: "Rubik", sans-serif;
  margin: 10px 0;
}

ul {
  display: flex;
  justify-content: center;
  align-items: center;
  list-style: none;
  gap: 10px;
  flex-wrap: wrap;

  padding: 0;
}

ul li {
  flex-grow: 1;
  padding: 15px;
  border-radius: 5px;

  box-shadow: 1px 2px 3px rgba(0 0 0 /0.5);
  color: #a52a2a;
  font-weight: bold;
  text-align: center;

  cursor: pointer;
}

ul li:hover {
  background: #81061c;
  color: #ffffff;
}

.container {
  display: flex;
  flex-direction: column;
  max-width: 320px;
  margin: 30px auto;
}

/*  adding fade-in animation */
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* adding fade-out animation */
@keyframes fadeOut {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

/* smooth transition */
.transition {
  transition: all 0.3s ease;
}

/* sorting buttons container */
.sort-buttons-container {
  margin: 20px 0;
}

#empty-list {
  display: none;
  text-align: center;
  font-style: italic;
  color: gray;
}

/* toast container */
#toast-container {
  position: fixed;
  z-index: 9999;
}

/*  toast message */
.toast {
  display: inline-block;
  padding: 12px 20px;
  background-color: #333;
  color: #fff;
  border-radius: 4px;
  opacity: 1;
  transition: opacity 0.1s ease-in-out;
}

/* toast message animation */
.toast.show {
  opacity: 1;
}

/* bought item */
.bought {
  opacity: 0.5;
  text-decoration: line-through;
}

#action-button {
  display: flex;
  padding-inline: 0.3em;
}

/* add / clear button style */
#add-button,
#clear-button {
  color: #fdfdfd;
  background-color: #ac485a;
  border: 0;
  padding: 15px;
  border-radius: 8px;
  font-size: 20px;
  text-align: center;
  font-family: "Rubik", sans-serif;
  cursor: pointer;
}

#clear-button {
  margin-left: auto;
}

#add-button:hover {
  background-color: #8a2b3d;
  font-weight: bold;
}

#clear-button:hover {
  background: #a50303;
  font-weight: bold;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Feel free to customize the styles according to your preferences. You can change the colors, fonts, and layout to match your design aesthetic.&lt;/p&gt;




&lt;h3&gt;
  
  
  Implementing the JavaScript Functionality
&lt;/h3&gt;

&lt;p&gt;Now comes the exciting part – implementing the JavaScript functionality for our shopping list app. Here's the JavaScript code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//selectors
const addButtonEl = document.querySelector("#add-button");
const emptyListMsg = document.querySelector("#empty-list");
const inputFieldEl = document.querySelector("#input-field");
const clearListButton = document.querySelector("#clear-button");
const toastContainer = document.querySelector("#toast-container");
let shoppingListEl = document.querySelector("#shopping-list");

let isPageReload = false;

addButtonEl.addEventListener("click", function () {
  let inputValue = inputFieldEl.value;

  if (inputValue !== "") {
    addItemToShoppingList(inputValue); // Add item to shopping list
    clearInputField();
  }
});

// Load initial shopping list from localStorage
loadShoppingList();

function loadShoppingList() {
  isPageReload = true;
  const shoppingListFromLocalStorage = localStorage.getItem("shoppingList");
  if (shoppingListFromLocalStorage) {
    let itemsArr = JSON.parse(shoppingListFromLocalStorage);
    clearAddToShoppingList();

    if (itemsArr.length &amp;gt; 0) {
      for (let i = 0; i &amp;lt; itemsArr.length; i++) {
        let currentItem = itemsArr[i].value; // Access the value property of the item object
        addItemToShoppingList(currentItem);
      }
    }
  }

  isPageReload = false;
  clearInputField(); // Clear the input field on page load
  updateEmptyListState();
}

function clearAddToShoppingList() {
  shoppingListEl.innerHTML = "";
  updateEmptyListState();
  localStorage.removeItem("shoppingList");
}

function clearInputField() {
  inputFieldEl.value = "";
}

//Add item to shopping list
function addItemToShoppingList(itemValue) {
  let itemId = Date.now().toString(); // Generate a unique ID for the item

  let item = {
    id: itemId,
    value: itemValue,
  };

  let itemsArr = [];

  if (localStorage.getItem("shoppingList")) {
    itemsArr = JSON.parse(localStorage.getItem("shoppingList"));
  }

  // Check if the item already exists in the shopping list
  const existingItem = itemsArr.some(
    (existingItem) =&amp;gt; existingItem.value === itemValue
  );

  if (existingItem) {
    showToast("Item already exists!", true); // Display a toast message indicating the item already exists
    return; // Exit the function to avoid adding the item again
  }

  itemsArr.push(item);
  localStorage.setItem("shoppingList", JSON.stringify(itemsArr));

  clearInputField(); // Clear the input field

  if (!isPageReload) {
    showToast("Item added successfully");
  }

  createItemElement(item);
  updateEmptyListState(); // Update the empty list state
}

//Display the shopping list
function createItemElement(item) {
  let itemEl = document.createElement("li");
  itemEl.textContent = item.value;
  itemEl.classList.add("transition"); // Add transition class for smooth effect

  itemEl.addEventListener("dblclick", function () {
    removeItemFromShoppingList(item.id);
    itemEl.remove();
    showToast("Item removed successfully!");
    updateEmptyListState(); // Update the empty list state
  });

  // Add fade-in animation when adding item
  setTimeout(() =&amp;gt; {
    itemEl.classList.add("fadeIn");
  }, 0); // Add the fade-in class immediately after creating the element

  shoppingListEl.append(itemEl);
}

//Remove item from the shopping list
function removeItemFromShoppingList(itemId) {
  let itemsArr = [];

  if (localStorage.getItem("shoppingList")) {
    itemsArr = JSON.parse(localStorage.getItem("shoppingList"));
  }

  itemsArr = itemsArr.filter((item) =&amp;gt; item.id !== itemId);
  localStorage.setItem("shoppingList", JSON.stringify(itemsArr));
  // Update the empty list state
  updateEmptyListState();
}

// show toast message
function showToast(message, isError = false) {
  //Array.from() search for an existing toast message with the same content
  const existingToast = Array.from(toastContainer.children).find(
    (toast) =&amp;gt; toast.textContent === message
  );
  if (existingToast) {
    return;
  }

  const toast = document.createElement("div");
  toast.className = `toast ${isError ? " error" : ""}`;
  toast.textContent = message;

  toastContainer.appendChild(toast);

  toast.classList.add("show");

  setTimeout(function () {
    toast.classList.remove("show");
    setTimeout(function () {
      toast.remove();
    }, 300); // Remove the toast from the DOM after the animation duration
  }, 500); // Delay the toast display to ensure smooth animation
}

// mark an item as bought
function markItemAsBought(itemEl) {
  if (!itemEl.classList.contains("bought")) {
    itemEl.classList.add("bought");
  }
}

// Event listener for clicking on an item
shoppingListEl.addEventListener("click", function (event) {
  const clickedItem = event.target;

  if (clickedItem.tagName === "LI") {
    markItemAsBought(clickedItem);
  }
});

// Update the empty list state
function updateEmptyListState() {
  const shoppingListFromLocalStorage = localStorage.getItem("shoppingList");
  const itemsArr = shoppingListFromLocalStorage
    ? JSON.parse(shoppingListFromLocalStorage)
    : null;

  if (itemsArr?.length &amp;gt; 0) {
    emptyListMsg.style.display = "none";
  } else {
    emptyListMsg.style.display = "block";
  }
}

clearListButton.addEventListener("click", function () {
  localStorage.removeItem("shoppingList");
  clearAddToShoppingList();
  showToast("Cart cleared successfully!");
  updateEmptyListState();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The JavaScript code is responsible for handling user interactions, managing the shopping list data, and updating the user interface accordingly. We'll break down the code into sections and explain each part in detail.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Adding Items to the Shopping List:&lt;/strong&gt;
One of the essential features of our app is the ability to add items to the shopping list. Let's start by adding the necessary code:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Add item to shopping list
function addItemToShoppingList(itemValue) {
  let itemId = Date.now().toString(); // Generate a unique ID for the item

  let item = {
    id: itemId,
    value: itemValue,
  };

  let itemsArr = [];

  if (localStorage.getItem("shoppingList")) {
    itemsArr = JSON.parse(localStorage.getItem("shoppingList"));
  }

  // Check if the item already exists in the shopping list
  const existingItem = itemsArr.some(
    (existingItem) =&amp;gt; existingItem.value === itemValue
  );

  if (existingItem) {
    showToast("Item already exists!", true); // Display a toast message indicating the item already exists
    return; // Exit the function to avoid adding the item again
  }

  itemsArr.push(item);
  localStorage.setItem("shoppingList", JSON.stringify(itemsArr));

  clearInputField(); // Clear the input field

  if (!isPageReload) {
    showToast("Item added successfully");
  }

  createItemElement(item);
  updateEmptyListState(); // Update the empty list state
}

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

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;addItemToShoppingList&lt;/code&gt; function takes an itemValue as a parameter and adds it to the shopping list. It also handles checking for duplicate items and displays a toast message to notify the user.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Displaying the Shopping List:&lt;/strong&gt;
Next, let's implement the code to display the shopping list on the screen:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createItemElement(item) {
  let itemEl = document.createElement("li");
  itemEl.textContent = item.value;
  itemEl.classList.add("transition"); // Add transition class for smooth effect

  itemEl.addEventListener("dblclick", function () {
    removeItemFromShoppingList(item.id);
    itemEl.remove();
    showToast("Item removed successfully!");
    updateEmptyListState(); // Update the empty list state
  });

  // Add fade-in animation when adding item
  setTimeout(() =&amp;gt; {
    itemEl.classList.add("fadeIn");
  }, 0); // Add the fade-in class immediately after creating the element

  shoppingListEl.append(itemEl);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;createItemElement&lt;/code&gt; function creates a new list item element for each item in the shopping list and appends it to the DOM. It also handles the removal of items when double-clicked and updates the empty list state.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Removing Items from the Shopping List:&lt;/strong&gt;
Now, let's add the code to remove items from the shopping list:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Remove item from the shopping list
function removeItemFromShoppingList(itemId) {
  let itemsArr = [];

  if (localStorage.getItem("shoppingList")) {
    itemsArr = JSON.parse(localStorage.getItem("shoppingList"));
  }

  itemsArr = itemsArr.filter((item) =&amp;gt; item.id !== itemId);
  localStorage.setItem("shoppingList", JSON.stringify(itemsArr));
  // Update the empty list state
  updateEmptyListState();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;removeItemFromShoppingList&lt;/code&gt; function removes the item with the specified ID from the shopping list. It also updates the empty list state and displays a toast message to inform the user.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Updating the Empty List State:&lt;/strong&gt;
To provide a better user experience, let's add code to update the empty list state based on the presence of items in the shopping list:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function updateEmptyListState() {
  const shoppingListFromLocalStorage = localStorage.getItem("shoppingList");
  const itemsArr = shoppingListFromLocalStorage
    ? JSON.parse(shoppingListFromLocalStorage)
    : null;

  if (itemsArr?.length &amp;gt; 0) {
    emptyListMsg.style.display = "none";
  } else {
    emptyListMsg.style.display = "block";
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;updateEmptyListState&lt;/code&gt; function checks if the shopping list is empty and displays a message accordingly.&lt;/p&gt;




&lt;h3&gt;
  
  
  Creating the Site Manifest
&lt;/h3&gt;

&lt;p&gt;Let's create the site manifest file (&lt;code&gt;site.webmanifest&lt;/code&gt;) and define the necessary configuration. Here's the code for the &lt;code&gt;site.webmanifest&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "Cart Companion",
  "short_name": "CartCom",
  "icons": [
    {
      "src": "/android-chrome-192x192.png",
      "sizes": "192x192",
      "type": "image/png"
    },
    {
      "src": "/android-chrome-512x512.png",
      "sizes": "512x512",
      "type": "image/png"
    }
  ],
  "theme_color": "#EEF0F4",
  "background_color": "#EEF0F9",
  "display": "standalone"
}

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

&lt;/div&gt;



&lt;p&gt;In the manifest file, we've specified the app's name, short name, icons for different devices, theme color, background color, and display mode. Adjust the icon URLs, theme colors, and other properties as needed to match your app's design. This will help us add the site to our phone home screen.&lt;/p&gt;




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

&lt;p&gt;Congratulations! You've successfully built a simple shopping list app using &lt;code&gt;HTM&lt;/code&gt;L, &lt;code&gt;CSS&lt;/code&gt;, and &lt;code&gt;JavaScript&lt;/code&gt;. Throughout this tutorial, we covered the basics of web development, including HTML structure, CSS styling, and JavaScript functionality. You learned how to add and remove items from the list, display them on the screen, and persist the data using the browser's &lt;code&gt;localStorage&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Feel free to enhance this app by adding more features such as item quantities, editing functionality, or even integrating it with a backend server.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed this beginner-friendly tutorial and gained valuable insights into web development. Start practicing and exploring new ideas to strengthen your skills!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Get Hands-On&lt;/strong&gt;&lt;br&gt;
Ready to explore and enhance your web development skills further? Try adding new features to the shopping list app or build your own projects. Experiment with different CSS styles, implement search functionality or create a responsive design. The possibilities are endless!&lt;/p&gt;

&lt;p&gt;Remember, practice is the key to mastery. So keep coding, exploring, and building amazing web applications.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;br&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%2Fzyh80c1va93owa7hzq8a.gif" 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%2Fzyh80c1va93owa7hzq8a.gif" alt="Happiness" width="320" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Mastering the Rules of React Hooks</title>
      <dc:creator>Nwosa Tochukwu </dc:creator>
      <pubDate>Wed, 31 May 2023 15:40:00 +0000</pubDate>
      <link>https://forem.com/obere4u/mastering-the-rules-of-react-hooks-3ao5</link>
      <guid>https://forem.com/obere4u/mastering-the-rules-of-react-hooks-3ao5</guid>
      <description>&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;Explore the world of React Hooks where functional components are at the forefront and complex state management is made easy. This comprehensive guide will delve into the rules of React Hooks, revealing their potential and teaching you how to utilize them effectively. Join us on this journey as we uncover the key principles of React Hooks and witness their impact on the development of React applications.&lt;/p&gt;




&lt;h2&gt;
  
  
  Section 1: Understanding the Essence of React Hooks
&lt;/h2&gt;

&lt;p&gt;React Hooks have transformed the way we handle state and lifecycle management in functional components. Before we delve into the rules, let's grasp the essence of React Hooks. Hooks are functions that enable us to use state and other React features in functional components. They provide a more concise and intuitive way to handle complex logic, eliminating the need for class components. By understanding the core principles of React Hooks, we lay the foundation for mastering their usage.&lt;/p&gt;




&lt;h2&gt;
  
  
  Section 2: The Rules of React Hooks
&lt;/h2&gt;

&lt;p&gt;In order to fully utilize the capabilities of React Hooks, it's important to adhere to a set of guidelines that dictate their usage. These guidelines are in place to ensure appropriate state management, prevent performance problems, and maintain consistent code. Let's delve into the fundamental rules of React Hooks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rule of Hooks: &lt;strong&gt;&lt;em&gt;Hooks must be used only at the top level of functional components.&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Always call React hooks before the &lt;code&gt;return&lt;/code&gt; keyword of React functional components&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';

function MyComponent() {
  const [count, setCount] = useState(0);

  useEffect(() =&amp;gt; {
    // logic here
  }, [count]);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setCount(count + 1)}&amp;gt;Increment&amp;lt;/button&amp;gt;
      &amp;lt;p&amp;gt;Count: {count}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Rule of Hooks: &lt;strong&gt;&lt;em&gt;Only call hooks from inside React functional components&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';

const [name, setName] = useState("")

export default function myComponent() {
return (
  &amp;lt;div&amp;gt;
    // code here
  &amp;lt;/div&amp;gt;
 )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bad practice&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';


export default function myComponent() {

const [name, setName] = useState("")

return (
  &amp;lt;div&amp;gt;
    // code here
  &amp;lt;/div&amp;gt;
 )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Good practice&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dependency Array: &lt;strong&gt;&lt;em&gt;Properly managing dependencies with useEffect to avoid unnecessary re-renders.&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';

function MyComponent() {
  const [data, setData] = useState(null);

  useEffect(() =&amp;gt; {
    // Fetch data here
    // ...

    return () =&amp;gt; {
      // Cleanup logic here
    };
  }, []); // Empty dependency array to run effect only once

  return &amp;lt;div&amp;gt;{data}&amp;lt;/div&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code snippet represents a functional component using React Hooks. It declares a state variable called &lt;code&gt;data&lt;/code&gt; using the &lt;code&gt;useState&lt;/code&gt; hook, initialized with a value of &lt;code&gt;null&lt;/code&gt;. The &lt;code&gt;useEffect&lt;/code&gt; hook is used to perform side effects, such as fetching data, within the component. In this example, the effect runs only once when the component mounts because the dependency array is empty (&lt;code&gt;[]&lt;/code&gt;). The code for fetching data and any cleanup logic can be placed inside the effect. The fetched data is stored in the &lt;code&gt;data&lt;/code&gt; state, and it is rendered within a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; element.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Custom Hooks: &lt;strong&gt;&lt;em&gt;Extracting reusable logic into custom hooks for cleaner and more maintainable code&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';

function useFetchData(url) {
  const [data, setData] = useState(null);
  const [isLoading, setIsLoading] = useState(true);

  useEffect(() =&amp;gt; {
    const fetchData = async () =&amp;gt; {
      try {
        const response = await fetch(url);
        const result = await response.json();
        setData(result);
        setIsLoading(false);
      } catch (error) {
        console.error('Error fetching data:', error);
        setIsLoading(false);
      }
    };

    fetchData();
  }, [url]);

  return { data, isLoading };
}

function MyComponent() {
  const { data, isLoading } = useFetchData('https://api.example.com/data');

  if (isLoading) {
    return &amp;lt;div&amp;gt;Loading...&amp;lt;/div&amp;gt;;
  }

  return (
    &amp;lt;div&amp;gt;
      {data ? (
        &amp;lt;ul&amp;gt;
          {data.map((item) =&amp;gt; (
            &amp;lt;li key={item.id}&amp;gt;{item.name}&amp;lt;/li&amp;gt;
          ))}
        &amp;lt;/ul&amp;gt;
      ) : (
        &amp;lt;div&amp;gt;No data available&amp;lt;/div&amp;gt;
      )}
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code example showcases a custom hook named &lt;code&gt;useFetchData&lt;/code&gt;, which contains the logic for obtaining data from an API. The hook accepts a URL as a parameter and provides the retrieved data and a loading state as output. The &lt;code&gt;useEffect&lt;/code&gt; hook is used to initiate the data retrieval process when there is a change in the URL. This approach guarantees that the data is obtained correctly and that the component is updated in response.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;useState: &lt;em&gt;&lt;strong&gt;Handling state mutations correctly and leveraging the functional updates pattern.&lt;/strong&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';

function MyComponent() {
  const [count, setCount] = useState(0);

  const increment = () =&amp;gt; {
    setCount((prevCount) =&amp;gt; prevCount + 1);
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;button onClick={increment}&amp;gt;Increment&amp;lt;/button&amp;gt;
      &amp;lt;p&amp;gt;Count: {count}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code snippet represents a functional component using React Hooks. It declares a state variable called &lt;code&gt;count&lt;/code&gt; using the &lt;code&gt;useState&lt;/code&gt; hook, initialized with a value of &lt;code&gt;0&lt;/code&gt;. The &lt;code&gt;increment&lt;/code&gt; function is used to update the &lt;code&gt;count&lt;/code&gt; state by incrementing its previous value. When the button is clicked, the &lt;code&gt;increment&lt;/code&gt; function is called, updating the state and triggering a re-render. The current value of &lt;code&gt;count&lt;/code&gt; is displayed within a &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;useEffect: &lt;em&gt;&lt;strong&gt;Synchronizing side effects with component lifecycle events and cleanup.&lt;/strong&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';

function MyComponent() {
  const [data, setData] = useState(null);

  useEffect(() =&amp;gt; {
    // Fetch data here
    // ...

    return () =&amp;gt; {
      // Cleanup logic here
    };
  }, []);

  return &amp;lt;div&amp;gt;{data}&amp;lt;/div&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code snippet represents a functional component using React Hooks. It uses the &lt;code&gt;useState&lt;/code&gt; hook to declare a state variable called &lt;code&gt;data&lt;/code&gt;, initialized with &lt;code&gt;null&lt;/code&gt;. The &lt;code&gt;useEffect&lt;/code&gt; hook is used to perform side effects, such as data fetching, and it runs only once due to an empty dependency array. The fetched data is stored in the &lt;code&gt;data&lt;/code&gt; state variable. Finally, the component renders the &lt;code&gt;data&lt;/code&gt; variable within a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Error Handling: &lt;em&gt;&lt;strong&gt;Properly handling errors within useEffect and useState.&lt;/strong&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';

function MyComponent() {
  const [data, setData] = useState(null);
  const [error, setError] = useState(null);

  useEffect(() =&amp;gt; {
    const fetchData = async () =&amp;gt; {
      try {
        const response = await fetch('https://api.example.com/data');
        const result = await response.json();
        setData(result);
      } catch (error) {
        setError(error);
      }
    };

    fetchData();
  }, []);

  if (error) {
    return &amp;lt;div&amp;gt;Error: {error.message}&amp;lt;/div&amp;gt;;
  }

  return &amp;lt;div&amp;gt;{data}&amp;lt;/div&amp;gt;;
}

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

&lt;/div&gt;



&lt;p&gt;The code snippet uses React Hooks, specifically &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useEffect&lt;/code&gt;, to create a functional component called &lt;code&gt;MyComponent&lt;/code&gt;. The component fetches data from an &lt;code&gt;API&lt;/code&gt; endpoint using the fetch function and updates the state variables &lt;code&gt;data&lt;/code&gt; and &lt;code&gt;error&lt;/code&gt; accordingly. If an &lt;code&gt;error&lt;/code&gt; occurs during the data fetching process, it is stored in the error state variable. The component renders the fetched data if it exists or displays an error message if an error occurred.&lt;/p&gt;




&lt;h2&gt;
  
  
  Section 3:  Embracing the Power of React Hooks
&lt;/h2&gt;

&lt;p&gt;By adhering to the rules of React Hooks, we unlock their full potential and empower ourselves to write clean, efficient, and maintainable code. With the ability to handle state, lifecycle events, and side effects seamlessly, React Hooks revolutionize the way we approach React development. By embracing React Hooks, we unleash the true power of functional components and elevate our React applications to new heights.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;Congratulations! You have now mastered the rules of React Hooks, paving the way for more effective and enjoyable React development. Remember to always follow the rules of Hooks to ensure proper state management, optimize performance, and maintain code consistency. As you continue your journey with React, embrace the power of Hooks and explore the vast possibilities they offer. Happy coding, and may your React applications thrive with the magic of React Hooks!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>react</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Simplifying React Props Drilling: A Beginner's Guide</title>
      <dc:creator>Nwosa Tochukwu </dc:creator>
      <pubDate>Wed, 17 May 2023 10:01:07 +0000</pubDate>
      <link>https://forem.com/obere4u/simplifying-react-props-drilling-a-beginners-guide-3kpc</link>
      <guid>https://forem.com/obere4u/simplifying-react-props-drilling-a-beginners-guide-3kpc</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;If you're a React developer, you've probably come across the term &lt;strong&gt;props drilling.&lt;/strong&gt; &lt;strong&gt;&lt;em&gt;It's a common pattern used to pass data down from a parent component to a child component in React.&lt;/em&gt;&lt;/strong&gt; While it's a powerful technique, it can also be confusing and overwhelming for beginners.&lt;/p&gt;

&lt;p&gt;In this article, we'll break down props drilling into simple, easy-to-understand terms, and provide you with tips and tricks to simplify your code.&lt;/p&gt;




&lt;h2&gt;
  
  
  Section 1: What is Prop drilling?
&lt;/h2&gt;

&lt;p&gt;In React, components can pass data to each other through props. &lt;strong&gt;Props drilling occurs when a component needs to pass data down to a child component that is not an immediate descendant.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For example, let's say you have a component called "Grandparent" that needs to pass some data to a component called "Grandchild." However, the data needs to go through a component called "Parent" first. In this scenario, the data would need to be passed from Grandparent to Parent, and then from Parent to Grandchild. This is props drilling.&lt;/p&gt;




&lt;h2&gt;
  
  
  Section 2: The Problem with Props Drilling
&lt;/h2&gt;

&lt;p&gt;Props drilling can quickly become cumbersome, especially if you have multiple levels of nesting. &lt;strong&gt;Passing data through multiple components can lead to "&lt;em&gt;prop hell&lt;/em&gt;,"&lt;/strong&gt; where you end up with a long chain of props that need to be passed down.&lt;/p&gt;

&lt;p&gt;Additionally, &lt;strong&gt;props drilling can make your code more brittle and harder to maintain&lt;/strong&gt;. If you need to add a new component in the middle of your chain, you'll need to update all the components that come after it to pass the new prop down.&lt;/p&gt;




&lt;h2&gt;
  
  
  Section 3: Simplifying Props Drilling
&lt;/h2&gt;

&lt;p&gt;So, how can we simplify props drilling? Here are a few tips&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Using &lt;em&gt;Context&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Context&lt;/strong&gt;&lt;/em&gt; is a powerful feature in React that &lt;strong&gt;allows you to pass data down the component tree without explicitly passing props.&lt;/strong&gt; Context provides a way to share data between components without having to pass props manually at every level.&lt;/p&gt;

&lt;p&gt;Here's an example of using context to simplify props drilling:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Create a context
const MyContext = React.createContext();

// Parent component providing data
function ParentComponent() {
  const data = "Hello from Parent";

  return (
    &amp;lt;MyContext.Provider value={data}&amp;gt;
      &amp;lt;ChildComponent /&amp;gt;
    &amp;lt;/MyContext.Provider&amp;gt;
  );
}

// Child component consuming data
function ChildComponent() {
  return (
    &amp;lt;MyContext.Consumer&amp;gt;
      {(data) =&amp;gt; &amp;lt;p&amp;gt;{data}&amp;lt;/p&amp;gt;}
    &amp;lt;/MyContext.Consumer&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;In this example, the ParentComponent provides the data through MyContext.Provider, and the ChildComponent consume it using the MyContext.Consumer. This way, you can access the data in the ChildComponent without manually passing it through intermediate components.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;Leveraging Redux:
&lt;strong&gt;Redux&lt;/strong&gt;, a popular state management library for React, can be a game-changer when dealing with the complex data flow. It &lt;strong&gt;provides a centralized store that holds your application's state, eliminating the need for props drilling&lt;/strong&gt;. With Redux, you can access the required data directly from the store, making your code cleaner and more maintainable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's an example on how to use Redux to simplify props drilling&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Define Redux actions and reducers

// Parent component dispatching an action
function ParentComponent() {
  const data = "Hello from Parent";
  dispatch(setData(data));
  // ...

  return &amp;lt;ChildComponent /&amp;gt;;
}

// Child component accessing data from the Redux store
function ChildComponent() {
  const data = useSelector((state) =&amp;gt; state.data);

  return &amp;lt;p&amp;gt;{data}&amp;lt;/p&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, the ParentComponent dispatches an action to set the data in the Redux store, and the ChildComponent accesses the data using the useSelector hook. Redux handles the data flow, simplifying the props drilling process.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;Harnessing Higher-Order Components (HOCs)
&lt;strong&gt;Higher-Order Components (HOCs) are functions that take a component and return an enhanced version of it with additional capabilities.&lt;/strong&gt; HOCs can be employed to inject props into components at any level of the component tree, thereby reducing the need for manual props drilling. &lt;strong&gt;By abstracting away the repetitive prop parsing logic, HOCs promote code reusability and enhance readability.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's an example of using an HOC to simplify props drilling:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Define the HOC
function withData(Component) {
  return function WithData(props) {
    const data = "Hello from HOC";

    return &amp;lt;Component data={data} {...props} /&amp;gt;;
  };
}

// Child component enhanced with the HOC
function ChildComponent(props) {
  return &amp;lt;p&amp;gt;{props.data}&amp;lt;/p&amp;gt;;
}

// Usage of the enhanced component
const EnhancedChildComponent = withData(ChildComponent);

// Render the enhanced component
function App() {
  return &amp;lt;EnhancedChildComponent /&amp;gt;;
}

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

&lt;/div&gt;



&lt;p&gt;In this example, the withData HOC injects the data prop into the ChildComponent, simplifying the need to manually pass it through the component hierarchy.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt; Choosing the Right Approach
When faced with the decision of which technique to use, consider the specific requirements of your project. &lt;strong&gt;If you have a small-scale application with simple data-sharing needs, Context might be the most straightforward option. For larger applications with more complex state management requirements, Redux is an excellent choice. HOCs can be utilized when you need fine-grained control over props injection.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Props drilling is a vital concept in React development, enabling the passage of data from a parent component to any level of child components. By understanding the challenges associated with props drilling and implementing techniques like Context, Redux, or HOCs, you can simplify your code and enhance maintainability. Remember, the goal is to make your code concise, readable, and efficient. So, choose the approach that best aligns with your project's needs, and embrace the power of props drilling in your React applications.&lt;/p&gt;




&lt;p&gt;Thank you for reading...&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>JavaScript topics to understand before moving to JavaScript libraies</title>
      <dc:creator>Nwosa Tochukwu </dc:creator>
      <pubDate>Tue, 16 May 2023 19:27:15 +0000</pubDate>
      <link>https://forem.com/obere4u/javascript-topics-to-understand-before-moving-to-javascript-libraies-agk</link>
      <guid>https://forem.com/obere4u/javascript-topics-to-understand-before-moving-to-javascript-libraies-agk</guid>
      <description>&lt;p&gt;Before diving into JavaScript libraries, it's important for beginners to have a solid understanding of the core JavaScript concepts and fundamentals. Here are some key topics you should familiarize yourself with:&lt;/p&gt;




&lt;h2&gt;
  
  
  1) Variables and Data Types:
&lt;/h2&gt;

&lt;p&gt;Understand how to declare variables, work with different data types (strings, numbers, booleans), and perform basic operations.&lt;/p&gt;




&lt;h2&gt;
  
  
  2) Functions:
&lt;/h2&gt;

&lt;p&gt;Learn how to create and use functions, including parameters, return values, and function expressions.&lt;/p&gt;




&lt;h2&gt;
  
  
  3) Control Flow:
&lt;/h2&gt;

&lt;p&gt;Understand conditional statements (if/else, switch), loops (for, while), and how to control the flow of your program.&lt;/p&gt;




&lt;h2&gt;
  
  
  4) Arrays:
&lt;/h2&gt;

&lt;p&gt;Familiarize yourself with arrays, their properties, and methods for manipulation (e.g., push, pop, splice).&lt;/p&gt;




&lt;h2&gt;
  
  
  5) Objects:
&lt;/h2&gt;

&lt;p&gt;Understand object-oriented programming concepts in JavaScript, including creating objects, accessing properties, and using methods.&lt;/p&gt;




&lt;h2&gt;
  
  
  6) DOM Manipulation:
&lt;/h2&gt;

&lt;p&gt;Learn how to interact with the Document Object Model (DOM) to manipulate HTML elements, modify content, and handle events.&lt;/p&gt;




&lt;h2&gt;
  
  
  7) Event Handling:
&lt;/h2&gt;

&lt;p&gt;Understand how to handle user interactions and respond to events triggered by users or the browser.&lt;/p&gt;




&lt;h2&gt;
  
  
  8) Asynchronous JavaScript:
&lt;/h2&gt;

&lt;p&gt;Explore asynchronous programming concepts such as callbacks, promises, and async/await to work with asynchronous tasks, such as making API requests.&lt;/p&gt;




&lt;h2&gt;
  
  
  9) Error Handling:
&lt;/h2&gt;

&lt;p&gt;Learn how to handle and manage errors in JavaScript using try/catch blocks and understanding common error types.&lt;/p&gt;




&lt;h2&gt;
  
  
  10) Modules:
&lt;/h2&gt;

&lt;p&gt;Familiarize yourself with JavaScript modules, which allow you to organize and reuse code, and understand how to import/export modules.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By gaining a strong foundation in these core JavaScript concepts, you'll be better prepared to grasp JavaScript libraries, as they often build upon these fundamentals. &lt;/p&gt;

&lt;p&gt;Remember to practice coding and work on small projects to reinforce your understanding.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Unleashing the Power of Async() and Await: Revolutionizing Asynchronous JavaScript</title>
      <dc:creator>Nwosa Tochukwu </dc:creator>
      <pubDate>Sun, 14 May 2023 14:13:47 +0000</pubDate>
      <link>https://forem.com/obere4u/unleashing-the-power-of-async-and-await-revolutionizing-asynchronous-javascript-1d8m</link>
      <guid>https://forem.com/obere4u/unleashing-the-power-of-async-and-await-revolutionizing-asynchronous-javascript-1d8m</guid>
      <description>&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;Welcome, JavaScript enthusiasts! Prepare to embark on a thrilling journey into the realm of asynchronous code, where we unravel the secrets of the captivating duo: &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt;. In this exhilarating primer, we will explore how these powerful keywords transform the way we write asynchronous JavaScript, making our code more elegant, readable, and efficient. So fasten your seatbelts and get ready to unlock the true potential of &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt;!&lt;/p&gt;




&lt;h3&gt;
  
  
  Section 1: Decoding the Enigma: Understanding async and await
&lt;/h3&gt;

&lt;p&gt;In the vast realm of JavaScript, asynchronous code reigns supreme. But fear not, for &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt; are here to rescue us from the complexities of callbacks and Promises. &lt;strong&gt;These ingenious keywords allow us to create asynchronous functions that seamlessly handle background operations without blocking the main thread.&lt;/strong&gt; The result? A smoother and more responsive user experience. Let's dive deeper into the magical world of &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Section 2: The Dynamic Duo: &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Enter the &lt;code&gt;async&lt;/code&gt; keyword, our gateway to the asynchronous world. &lt;strong&gt;By using &lt;code&gt;async&lt;/code&gt; before a function declaration, we signal that it will execute asynchronously, allowing it to return a Promise. This clever addition sets the stage for the show's true star – &lt;code&gt;await&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;await&lt;/code&gt; holds the power to halt the execution of an &lt;code&gt;async&lt;/code&gt; function until a Promise is resolved.&lt;/strong&gt; Picture a conductor pausing a symphony, allowing other musicians to catch up. With &lt;code&gt;await&lt;/code&gt;, we gracefully synchronize our code, making it read like a symphony of logic. Gone are the days of convoluted promise chains and callback pyramids. Let's see &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt; in action!&lt;/p&gt;




&lt;h3&gt;
  
  
  Section 3: Elevating Asynchronous Operations: A Practical Example
&lt;/h3&gt;

&lt;p&gt;Imagine fetching a list of users from the GitHub API. Traditionally, we would wrestle with promises and a complex syntax. But fear not, for &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt; come to the rescue:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function getUsers() {
  const response = await fetch('https://api.github.com/users');
  const users = await response.json();
  return users;
}

const users = await getUsers();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Behold the elegance! In just a few lines of code, we fetch data from the API, gracefully handle the promise resolution, and return an array of users. The power of &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt; lies in their ability to simplify complex asynchronous operations into digestible, readable code.&lt;/p&gt;




&lt;h3&gt;
  
  
  Section 4: Unleash the Potential: Embrace &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;With the advent of &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt;, writing asynchronous JavaScript has become an art form. It empowers developers to create clean, concise, and maintainable code, paving the way for enhanced collaboration and accelerated development. If you haven't explored the realm of &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt; yet, now is the time! Unleash the potential of your code and revolutionize your asynchronous JavaScript.&lt;/p&gt;




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

&lt;p&gt;As we bid farewell to the land of callbacks and bidirectional promises, we embrace the power of &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt;. These remarkable keywords have ushered in a new era of asynchronous JavaScript, &lt;strong&gt;transforming complex code into an elegant symphony of efficiency.&lt;/strong&gt; So, dear developers, immerse yourself in the world of &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt;, and let your code dance to the rhythm of simplicity and readability. Share your experiences and insights in the comments below – let's build a vibrant community of &lt;code&gt;async&lt;/code&gt; aficionados!&lt;/p&gt;

&lt;p&gt;Happy Coding.....&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Learn to use useState and useEffect hooks in React to create dynamic user interfaces</title>
      <dc:creator>Nwosa Tochukwu </dc:creator>
      <pubDate>Fri, 05 May 2023 13:33:39 +0000</pubDate>
      <link>https://forem.com/obere4u/learn-to-use-usestate-and-useeffect-hooks-in-react-to-create-dynamic-user-interfaces-20dj</link>
      <guid>https://forem.com/obere4u/learn-to-use-usestate-and-useeffect-hooks-in-react-to-create-dynamic-user-interfaces-20dj</guid>
      <description>&lt;p&gt;React is a widely used JavaScript library for building user interfaces, with &lt;strong&gt;useState&lt;/strong&gt; and &lt;strong&gt;useEffect&lt;/strong&gt; being two of its most important features&lt;/p&gt;

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

&lt;p&gt;Imagine you have a toy box with many toys in it. Sometimes you want to play with a specific toy, so you take it out of the box and play with it. When you're done playing with it, you put it back in the box. This is how useState works in React!&lt;br&gt;
**useState **is a way to store and update values (like toys in a toy box) within a component. You can use it to keep track of things like whether a button has been clicked or the value of a form input.&lt;br&gt;
Here's an example:&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="s1"&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;function&lt;/span&gt; &lt;span class="nf"&gt;Counter&lt;/span&gt;&lt;span class="p"&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&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;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;You&lt;/span&gt; &lt;span class="nx"&gt;clicked&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;times&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;p&gt;This example uses useState in React to keep track of the number of times a button has been clicked. We start the count at 0 by passing it as an argument to useState, and we update the count using the setCount function whenever the button is clicked.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is useEffect?
&lt;/h2&gt;

&lt;p&gt;In React, useEffect is like watering a plant every day to keep it healthy.&lt;br&gt;
&lt;strong&gt;useEffect&lt;/strong&gt; is a hook that enables us to execute side effects in a component. Side effects include tasks that interact with external systems, such as fetching data from an API, updating the document title, or setting up event listeners. With "useEffect", you can guarantee that your side effects are executed at the appropriate times during the component's lifecycle, such as after it has mounted or updated. This makes it a valuable tool for managing state and behavior in React applications.&lt;br&gt;
Here's an example:&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="nx"&gt;useEffect&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="s1"&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;function&lt;/span&gt; &lt;span class="nf"&gt;Greeting&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&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;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setMessage&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Loading...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&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;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`https://api.example.com/greeting?name=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&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;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setMessage&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;message&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="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;We're using useEffect to fetch a greeting from an API and update the message state whenever the &lt;code&gt;name&lt;/code&gt; prop changes. We pass an array containing the &lt;code&gt;name&lt;/code&gt; prop to useEffect, which tells React to re-run the effect whenever the prop changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;useState&lt;/strong&gt; and &lt;strong&gt;useEffect&lt;/strong&gt; are important features in React. &lt;strong&gt;useState stores and updates component values&lt;/strong&gt;, while &lt;strong&gt;useEffect runs side effects&lt;/strong&gt;. Together, they create dynamic and interactive user interfaces that respond to user input and update automatically.&lt;/p&gt;

&lt;p&gt;I hope this helped you understand useState and useEffect in React!&lt;/p&gt;




&lt;p&gt;My name is &lt;strong&gt;Tochukwu Nwosa&lt;/strong&gt;, a junior &lt;strong&gt;frontend developer&lt;/strong&gt; with React&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>A Fun Introduction to padStart() in JavaScript: Padding Your Strings Like a Pro</title>
      <dc:creator>Nwosa Tochukwu </dc:creator>
      <pubDate>Mon, 01 May 2023 16:27:28 +0000</pubDate>
      <link>https://forem.com/obere4u/a-fun-introduction-to-padstart-in-javascript-padding-your-strings-like-a-pro-3oi</link>
      <guid>https://forem.com/obere4u/a-fun-introduction-to-padstart-in-javascript-padding-your-strings-like-a-pro-3oi</guid>
      <description>&lt;p&gt;In JavaScript strings are a very important part of the code. Indeed, you can say they're the engines of a program. Sometimes, however, it's necessary to check that your strings contain a specified number of characters even if this requires filling in the first string with several zeroes or different characters. This is where &lt;code&gt;padStart()&lt;/code&gt; comes into play. Let's have some fun in this article, learning how to use &lt;code&gt;padStart()&lt;/code&gt;. Make our string look exactly the way we want it!&lt;/p&gt;

&lt;p&gt;To begin, &lt;strong&gt;&lt;code&gt;padStart()&lt;/code&gt; is a built-in method in JavaScript that adds characters to the beginning of a string until it reaches a desired length&lt;/strong&gt;. When you need to format numbers, make user IDs, or work with any kind of string that requires a certain number of characters, this can be helpful. Here an example&lt;br&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%2Fm58zwhylc8nvvuxsp8g7.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%2Fm58zwhylc8nvvuxsp8g7.png" alt="JavaScript padStart code" width="800" height="332"&gt;&lt;/a&gt;&lt;br&gt;
&lt;code&gt;padStart()&lt;/code&gt; is used to add five zeros to the beginning of the string "hello" in the code above. The &lt;strong&gt;first parameter of &lt;code&gt;padStart()&lt;/code&gt; specifies the total length of the new string, and the second parameter specifies the character to add to the string&lt;/strong&gt; until it reaches the desired length.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do we use padStart()?
&lt;/h2&gt;

&lt;p&gt;Let's look at some examples of how padStart() can be used in our code now that we know what it does&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Formatting numbers&lt;/strong&gt;
Let's say we want to show a number as a string with a certain number of digits. Until the string reaches the desired length, we can use padStart() to add zeros to its beginning:&lt;/li&gt;
&lt;/ul&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%2Fdvkxlvuq8of5kugbl65c.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%2Fdvkxlvuq8of5kugbl65c.png" alt="Using padStart to format numbers" width="800" height="343"&gt;&lt;/a&gt;&lt;br&gt;
In the above example, we, first of all, use &lt;code&gt;toString()&lt;/code&gt; to convert the number 42 to a string, and then use &lt;code&gt;padStart()&lt;/code&gt; to add three zeroes to the beginning of the string so that it has a total of five digits&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Creating user IDs&lt;/strong&gt;
Let's say we want to give each new user a unique user ID when we create a user registration form for a website. Using &lt;code&gt;padStart()&lt;/code&gt;, we can ensure that each user ID contains a predetermined number of digits
&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%2Fgp96x4a25iyatxvp50vn.png" alt="Creating User IDs with padStart()" width="800" height="422"&gt;
In the above example, we first set userId to 1 and then use &lt;code&gt;toString()&lt;/code&gt; and &lt;code&gt;padStart()&lt;/code&gt; to add three zeroes to the beginning of the string so that it has a total of four digits. We then set userId to 1234 and use &lt;code&gt;padStart()&lt;/code&gt; again to make sure it has a total of four digits, but this time no zeroes are added because the string already has four digits.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In conclusion, &lt;strong&gt;the JavaScript method &lt;code&gt;padStart()&lt;/code&gt; can be used to format strings and ensure that they contain a predetermined number of characters&lt;/strong&gt;. We've seen a few ways to use &lt;code&gt;padStart()&lt;/code&gt; in our code, but there is no end of possibilities. Consider using &lt;code&gt;padStart()&lt;/code&gt; the next time you work with strings in JavaScript to ensure that they are formatted correctly.&lt;/p&gt;

&lt;p&gt;We talked about &lt;code&gt;padStart()&lt;/code&gt; and how it works in this article. We discovered that the method &lt;strong&gt;&lt;code&gt;padStart()&lt;/code&gt; lets us add leading characters to a string until it reaches a predetermined length.&lt;/strong&gt; We also saw some examples of how to use &lt;code&gt;padStart()&lt;/code&gt; with template literals and format strings with leading zeros.&lt;/p&gt;

&lt;p&gt;Here's a rundown of what we talked about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We can add leading characters to a string until it reaches a certain length using the &lt;code&gt;padStart()&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;The total length we want the string to have is the first argument passed to &lt;code&gt;padStart()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The character we want to use as padding is the second argument of &lt;code&gt;padStart()&lt;/code&gt;. It is, by default, a space.&lt;/li&gt;
&lt;li&gt;When working with dates and times, we can format strings with leading zeros using &lt;code&gt;padStart()&lt;/code&gt;.
&lt;strong&gt;&lt;code&gt;PadStart()&lt;/code&gt; can also be used to format strings with dynamic values using template literals.&lt;/strong&gt;
In general, &lt;code&gt;padStart()&lt;/code&gt; is a powerful and adaptable tool that can assist us in formatting strings in several different ways. We can make our code easier to read and maintain by utilizing &lt;code&gt;padStart()&lt;/code&gt;, which is always a good thing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Did you find this article on &lt;code&gt;padStart()&lt;/code&gt; informative? If so, don't be shy - give it a like and drop a comment! Your feedback means the world to us and helps us to keep delivering great content&lt;/p&gt;

&lt;p&gt;My name is &lt;strong&gt;Tochukwu Nwosa&lt;/strong&gt; and I'm a Junior &lt;strong&gt;Front-end Dev (React)&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>DOMContentLoaded vs window.onload</title>
      <dc:creator>Nwosa Tochukwu </dc:creator>
      <pubDate>Mon, 01 May 2023 13:32:21 +0000</pubDate>
      <link>https://forem.com/obere4u/domcontentloaded-vs-windowonload-9mc</link>
      <guid>https://forem.com/obere4u/domcontentloaded-vs-windowonload-9mc</guid>
      <description>&lt;p&gt;You've been waiting for a friend to come and play, but they haven't come yet, you'd probably be bored and sad, wouldn't you?&lt;br&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%2Fkgdiufu3gtypj2six4kw.gif" 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%2Fkgdiufu3gtypj2six4kw.gif" alt="Sad Lonely GIF By Pokémon" width="1024" height="1024"&gt;&lt;/a&gt;That's a little like the way it works when you load your website. Your computer will have to open all the pictures, words, and videos that make up this website when you visit a Web site. It may take some time to put everything in order, especially if the site is large.&lt;br&gt;
That is exactly what &lt;code&gt;DOMContentLoaded&lt;/code&gt; and &lt;code&gt;window.onload&lt;/code&gt; wants to avoid by making sure that everything on the website is loaded properly so that visitors don't have to wait for a long time to see the website.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;window.onload&lt;/code&gt; is like a signal that tells the website that everything has finished loading&lt;/strong&gt;. It's like your friend finally showing up to play after you've been waiting for a long time.&lt;/p&gt;

&lt;p&gt;Here's an example of how window.onload works in code:&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="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// This code will only run when the page has finished loading&lt;/span&gt;
  &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;The website is fully loaded!&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The DOMContent Loaded is a little bit of a different. It's like a &lt;strong&gt;signal that tells the website that the basic structure has been loaded, but some of the pictures and videos may still be loading.&lt;/strong&gt; It feels like your friend's come, but so far they haven't brought all the toys. &lt;br&gt;
This is a code example of how DOMContentLoaded works.&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DOMContentLoaded&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// This code runs when the basic structure of the page has loaded&lt;/span&gt;
  &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;The website structure has loaded!&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The basic contents that need to be loaded before the DOMContentLoaded event is fired are the elements that are required for the page layout and structure to be visible and functional to the user, while the other contents such as images and videos are usually considered secondary contents that can be loaded later without affecting the functionality of the page.&lt;/p&gt;

&lt;p&gt;By using these events, website developers can make sure that the website loads as quickly as possible, and that visitors can start interacting with the website as soon as the basic structure is loaded. This helps to provide a better user experience and ensures that visitors don't get bored or irritated while waiting for the website to load.&lt;/p&gt;

&lt;p&gt;In summary, window.onload and DOMContentLoaded are &lt;strong&gt;signals that tell the website when everything is finished loading, or when the basic structure of the website is loaded&lt;/strong&gt;. It's like you're waiting for a friend to show up to play, and you're happy when he finally shows up.&lt;br&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%2Frlnbfu4w7d2ywt5pd85u.gif" 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%2Frlnbfu4w7d2ywt5pd85u.gif" alt="Happy Lets Go GIF By SpongeBob SquarePants" width="500" height="286"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you found this article helpful or informative, I would love to hear from you! Please feel free to leave a comment or like the post to let me know your thoughts. Your feedback is greatly appreciated. Thank you for reading!&lt;/p&gt;

&lt;p&gt;My name is &lt;strong&gt;Nwosa Tochukwu&lt;/strong&gt; and I'm a Junior &lt;strong&gt;Front-end developer (REACTJS)&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
