<?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>DEV Community: Geetika Bajpai</title>
    <description>The latest articles on DEV Community by Geetika Bajpai (@geetika_bajpai_a654bfd1e0).</description>
    <link>https://dev.to/geetika_bajpai_a654bfd1e0</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%2F1614613%2Fa05a2311-a933-45f8-a7a0-0b6902af84a5.png</url>
      <title>DEV Community: Geetika Bajpai</title>
      <link>https://dev.to/geetika_bajpai_a654bfd1e0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/geetika_bajpai_a654bfd1e0"/>
    <language>en</language>
    <item>
      <title>Why Cypress?</title>
      <dc:creator>Geetika Bajpai</dc:creator>
      <pubDate>Tue, 20 Aug 2024 18:06:09 +0000</pubDate>
      <link>https://dev.to/geetika_bajpai_a654bfd1e0/why-cypress-hf6</link>
      <guid>https://dev.to/geetika_bajpai_a654bfd1e0/why-cypress-hf6</guid>
      <description>&lt;ol&gt;
&lt;li&gt;An all in-in-one testing framework, assertion library, with mocking and stubbing.&lt;/li&gt;
&lt;li&gt;Focus on E2E and component testing -- real world testing.&lt;/li&gt;
&lt;li&gt;Runs in the browser and wrote in javascript.&lt;/li&gt;
&lt;li&gt;Good performance and can be integrated in CI/CD easily.&lt;/li&gt;
&lt;li&gt;Native access to the DOM and to your app.&lt;/li&gt;
&lt;li&gt;Great developer UX.&lt;/li&gt;
&lt;li&gt;Generally not flaky if you follow the best practices.&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>Basics about Backend</title>
      <dc:creator>Geetika Bajpai</dc:creator>
      <pubDate>Thu, 01 Aug 2024 19:06:22 +0000</pubDate>
      <link>https://dev.to/geetika_bajpai_a654bfd1e0/basics-about-backend-3jjo</link>
      <guid>https://dev.to/geetika_bajpai_a654bfd1e0/basics-about-backend-3jjo</guid>
      <description>&lt;p&gt;Backend has 2 major components:-&lt;br&gt;
&lt;strong&gt;1. A Programming Language:-&lt;/strong&gt; Java, javascript, php, golang, c++ etc. ( Almost all these languages use some framework or library).&lt;br&gt;
&lt;strong&gt;2. A Database:-&lt;/strong&gt; Mongo, MySQL, Postgres, sqlite etc. (Similarly, you will use an ORM (Object-Relational Mapping) or ODM (Object Document Mapping) when interacting with databases. Names like Prisma and Mongoose will come up).&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Frontend (Browser and Mobile):&lt;/strong&gt;Users interact with the application through the browser or mobile app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;API (Application Programming Interface):&lt;/strong&gt;Acts as an intermediary, allowing the frontend to communicate with the backend server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Backend:&lt;/strong&gt;Processes requests from the frontend, interacts with the database, and sends responses back.&lt;br&gt;
Contains the logic and functions needed to handle specific tasks like user authentication.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Database:&lt;/strong&gt;Stores application data and is located in another continent for better performance and redundancy.&lt;/p&gt;

&lt;p&gt;When dealing with backend development, you primarily handle three types of tasks:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.Data Handling:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The most common task in backend development is processing data received from the frontend.&lt;/li&gt;
&lt;li&gt;Example: When a user tries to log in, the frontend sends the username and password to the backend. The backend verifies these credentials against the database and sends a response back to the frontend.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2.File Handling:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Backends often need to handle file uploads and downloads. This can include storing files on the server or retrieving them for user access.&lt;/li&gt;
&lt;li&gt;Example: A user uploads a profile picture. The backend saves this image to the server and updates the user's profile to reference the uploaded file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3.Third-Party API Integration:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Backends frequently interact with third-party services to extend functionality.&lt;/li&gt;
&lt;li&gt;Example: To send an email confirmation, the backend may use an email service provider's API. The backend sends the necessary data (recipient email, subject, message body) to the third-party API, which then handles the email sending.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In modern JavaScript environments, there are several options for backend development beyond the traditional Node.js. Here are the key points:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Multiple Runtime Environments:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Historically, learning backend development in JavaScript often started with Node.js.&lt;/li&gt;
&lt;li&gt;Nowadays, there are alternative runtime environments like Deno and Bun.&lt;/li&gt;
&lt;li&gt;You can even mix environments, using part of your backend on Node.js and part on Deno or Bun.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The file structure I am referring to is a common organization pattern used in backend development, regardless of the specific programming language or framework. Here's a breakdown of the typical components based on the content you provided:&lt;/p&gt;

&lt;h2&gt;1. Root Directory&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;index.js or main.js:&lt;/strong&gt; This is the entry point of your application. It’s where you initialize and start your application, connect to the database, and set up basic configurations.&lt;/p&gt;

&lt;h2&gt;2. Folders and Files&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;app.js:&lt;/strong&gt;This file typically contains the main application logic and configuration settings, such as middleware setup, routes configuration, and application-level settings.&lt;/p&gt;

&lt;h3&gt;config/:&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Configuration Files:&lt;/strong&gt;These files include environment-specific settings such as database connections, API keys, and other constants.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt; config.js, databaseConfig.js.&lt;/p&gt;

&lt;h3&gt;constants/:&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;constants.js:&lt;/strong&gt;Contains immutable values or configurations used throughout the application. This can include things like status codes, roles, or types. For instance, if you're building a booking application, constants might include seat types (e.g., window, middle, aisle).&lt;/p&gt;

&lt;h3&gt;models/:&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Model Definitions:&lt;/strong&gt;Defines the schema for the data stored in your database. This is where you set up how your data is structured and validated.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;userModel.js, productModel.js.&lt;/p&gt;

&lt;h3&gt;controllers/:&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Controller Logic:&lt;/strong&gt; Contains functions or methods that handle business logic. Controllers manage the requests and responses, processing data and interacting with models.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt; userController.js, productController.js.&lt;/p&gt;

&lt;h3&gt;routes/:&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Routing Definitions:&lt;/strong&gt;Maps URLs to specific controller actions. This determines how different HTTP requests (GET, POST, etc.) are routed to the appropriate controllers.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;userRoutes.js, productRoutes.js.&lt;/p&gt;

&lt;h3&gt;middlewares/:&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Middleware Functions:&lt;/strong&gt;Contains functions that execute during the request-response cycle, such as authentication checks, logging, or data validation.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt; authMiddleware.js, errorHandlingMiddleware.js.&lt;/p&gt;

&lt;h3&gt;utils/:&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Utility Functions:&lt;/strong&gt; Houses reusable functions or helpers used across the application, such as email sending functions or file upload handlers.&lt;br&gt;
&lt;strong&gt;Example&lt;/strong&gt;emailHelper.js, fileUploadHelper.js.&lt;/p&gt;

&lt;h3&gt;database/:&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Database Initialization:&lt;/strong&gt;Contains scripts or configurations related to database setup and connections.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;dbConnection.js, dbSetup.js.&lt;/p&gt;

&lt;h3&gt;`views`/ (if applicable):&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Template Files:&lt;/strong&gt;Used if the application includes server-side rendering. This directory contains the view templates or files used to generate HTML content.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt; index.ejs, userProfile.ejs.&lt;/p&gt;

&lt;h2&gt;3. Other Files&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;package.json: Manages dependencies and scripts for the project.&lt;/li&gt;
&lt;li&gt;.env: Stores environment variables like database credentials or API keys.&lt;/li&gt;
&lt;li&gt;.gitignore: Specifies which files and directories should be ignored by version control&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>React Redux Toolkit</title>
      <dc:creator>Geetika Bajpai</dc:creator>
      <pubDate>Thu, 27 Jun 2024 19:24:54 +0000</pubDate>
      <link>https://dev.to/geetika_bajpai_a654bfd1e0/react-redux-toolkit-38c2</link>
      <guid>https://dev.to/geetika_bajpai_a654bfd1e0/react-redux-toolkit-38c2</guid>
      <description>&lt;p&gt;Problem Statement&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6zfm805o4rw509tbfwgs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6zfm805o4rw509tbfwgs.png" alt="Image description" width="657" height="593"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When working with React, you typically engage in larger projects. Otherwise, HTML and CSS suffice for simpler development needs. A substantial project involves numerous components and elements, as seen in an ecommerce website. Synchronizing these components—such as updating the cart and total upon clicking "Add to Cart"—can pose challenges.&lt;/p&gt;

&lt;p&gt;Without a redux this is how we implement this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmctvj7q1w0oi4p22bqr4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmctvj7q1w0oi4p22bqr4.png" alt="Image description" width="747" height="231"&gt;&lt;/a&gt;&lt;br&gt;
We are doing prop drilling here.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxjprc69zl5kd2lr3eykq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxjprc69zl5kd2lr3eykq.png" alt="Image description" width="692" height="550"&gt;&lt;/a&gt;&lt;br&gt;
Here, we observe tight coupling because each component relies on its parent to access state. The solution lies in implementing a single source of truth.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7bxprztirdxlohb79681.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7bxprztirdxlohb79681.png" alt="Image description" width="800" height="495"&gt;&lt;/a&gt;&lt;br&gt;
The concept of a "single source of truth" dictates that state components should not reside in the app component but rather in a centralized store accessible to any component. The cart subscribes to this store, ensuring that any changes trigger its re-rendering. On the product page, when an item is added, it updates the store, prompting the cart to re-render in response to these changes.&lt;/p&gt;

&lt;p&gt;Architecture of Redux &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw6ihi2goqrmqljnk8u73.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw6ihi2goqrmqljnk8u73.png" alt="Image description" width="797" height="327"&gt;&lt;/a&gt;&lt;br&gt;
React Redux is a popular library combination used in React applications to manage state and facilitate data flow. Here's a breakdown of its architecture:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;With the UI handler button clicked.&lt;/li&gt;
&lt;li&gt;From button goes to handler function and handler function dispatch event.&lt;/li&gt;
&lt;li&gt;Event goes to store(store does know what to do with event).&lt;/li&gt;
&lt;li&gt;Store give event to reducer with the current value of the state.&lt;/li&gt;
&lt;li&gt;Reducer will do the changes on the current value based on the event.&lt;/li&gt;
&lt;li&gt;Reducer return the new value to redux store.&lt;/li&gt;
&lt;li&gt;Redux Store will accept that new value.&lt;/li&gt;
&lt;li&gt;And those components who are listing that value or subscribed that will get update.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;Install Redux Toolkit and React-Redux&lt;/h4&gt;

&lt;p&gt;Add the Redux Toolkit and React-Redux packages to your project:&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyzen7l7k7mo6mmktoq8f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyzen7l7k7mo6mmktoq8f.png" alt="Image description" width="487" height="47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;Create a Redux Store&lt;/h4&gt;

&lt;p&gt;Create store.ts file inside store folder&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuskqs8inxnx1p0uw79ce.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuskqs8inxnx1p0uw79ce.png" alt="Image description" width="627" height="316"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;export const store = configureStore({ ... });&lt;/code&gt;: Calls the configureStore function to create a Redux store instance. It accepts an object with configuration options.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;reducer&lt;/code&gt;: An object that specifies how different slices of the application's state should be combined. In this case, the counterSlice is assigned to the cart key. This means that the counterSlice will manage the state stored under the cart slice of the Redux store.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The provided code sets up a Redux store using @reduxjs/toolkit's configureStore function. It configures the store to use a counterSlice to manage the state related to the cart. Finally, it exports the configured store instance so that it can be imported and used throughout the application to manage and access application state.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhd97ji01akf8x36bclsm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhd97ji01akf8x36bclsm.png" alt="Image description" width="800" height="471"&gt;&lt;/a&gt;&lt;br&gt;
 It renders the App component wrapped in  to provide access to the Redux store. The use of &lt;code&gt;ReactDOM.createRoot&lt;/code&gt; and &lt;code&gt;root.render(...)&lt;/code&gt; indicates the usage of React Concurrent Mode, allowing for more predictable rendering and performance optimizations in modern React applications.&lt;/p&gt;

&lt;p&gt;Now, we will make slices&lt;br&gt;
Every feature is a slice, a Redux state slice represents a modular, encapsulated part of the application state managed by Redux. It helps organize and manage state in a more structured and maintainable manner, particularly in larger applications with complex state requirements.&lt;/p&gt;

&lt;p&gt;We will create a folder slices inside redux folder and inside slices folder create counter folder and the we will create counter.ts file inside that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzahu2vcxg7bzo6qpbqdf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzahu2vcxg7bzo6qpbqdf.png" alt="Image description" width="626" height="305"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;initialState = 0: Specifies the initial state of the application, which is set to 0.&lt;/li&gt;
&lt;li&gt;The reducer handles different action types (INCREMENT and DECREMENT) by returning a new state based on the current state and the action type. If the action type does not match any case (default), it returns the current state unchanged.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The line &lt;code&gt;export const { increment, decrement, incrementByAmount } = counterSlice.actions&lt;/code&gt; simplifies the process of creating and exporting action creators from a Redux slice defined using Redux Toolkit's createSlice. It promotes cleaner and more concise Redux-related code by automatically generating these action creators based on defined reducers.&lt;/p&gt;

&lt;p&gt;Now create hooks inside index.ts which will create in a new folder created called hooks folder in redux folder.&lt;/p&gt;

&lt;p&gt;useDispatch and useSelector are React Redux hooks used for interacting with the Redux store in functional components.&lt;br&gt;
useDispatch: Hook used to obtain a reference to the Redux dispatch function, which is used to dispatch actions to the Redux store.&lt;br&gt;
useSelector: Hook used to extract and return data from the Redux store state. &lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F20arsvw1m1klq68gte16.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F20arsvw1m1klq68gte16.png" alt="Image description" width="368" height="382"&gt;&lt;/a&gt;&lt;br&gt;
we can do this with simple state then why we do this complex so let me show you why create &lt;code&gt;MyComp.ts&lt;/code&gt; in src&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxuggu0sez8f8flbpevng.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxuggu0sez8f8flbpevng.png" alt="Image description" width="552" height="257"&gt;&lt;/a&gt;&lt;br&gt;
So, here we create some dummy component.&lt;/p&gt;

&lt;p&gt;Then render my component there&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz6n0bqi3xhitd8qg5x0b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz6n0bqi3xhitd8qg5x0b.png" alt="Image description" width="692" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So here when we increment one component other also render because this MyComp is also using state means there components subscribed store&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv0xvne35rphxm53a6e3w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv0xvne35rphxm53a6e3w.png" alt="Image description" width="343" height="606"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>useMemo Hook</title>
      <dc:creator>Geetika Bajpai</dc:creator>
      <pubDate>Wed, 26 Jun 2024 20:50:39 +0000</pubDate>
      <link>https://dev.to/geetika_bajpai_a654bfd1e0/usememo-hook-ne8</link>
      <guid>https://dev.to/geetika_bajpai_a654bfd1e0/usememo-hook-ne8</guid>
      <description>&lt;p&gt;&lt;code&gt;useMemo&lt;/code&gt; is a hook that allows you to memoize expensive calculations to avoid unnecessary re-computations on every render.&lt;/p&gt;

&lt;h2&gt;
  
  
  Explanation of the Code
&lt;/h2&gt;

&lt;p&gt;This React component, MemoTutorial, demonstrates how to use the useMemo hook to optimize performance by memoizing the result of a function that computes the longest name from a list of comments fetched from an API. Let's break down the code step by step.&lt;/p&gt;

&lt;h4&gt;1. Imports:&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fznzboc5t85l61xk4t91v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fznzboc5t85l61xk4t91v.png" alt="Image description" width="521" height="71"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;axios is used to make HTTP requests.&lt;/li&gt;
&lt;li&gt;useEffect, useState, and useMemo are hooks from React.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;2. Component Definition:&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F38c55yh366v845f6wugx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F38c55yh366v845f6wugx.png" alt="Image description" width="411" height="45"&gt;&lt;/a&gt;&lt;br&gt;
The MemoTutorial function is the React component.&lt;/p&gt;

&lt;h4&gt;3. State Variables:&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6r66iqbca73yjsys9kky.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6r66iqbca73yjsys9kky.png" alt="Image description" width="436" height="72"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;data&lt;/code&gt;: Holds the comments fetched from the API.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;toggle&lt;/code&gt;: A boolean used to trigger re-renders.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;4. Fetching Data:&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx5kruu21p7lije1zluzr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx5kruu21p7lije1zluzr.png" alt="Image description" width="563" height="238"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;useEffect&lt;/code&gt; is used to fetch data from the API when the component mounts.&lt;/li&gt;
&lt;li&gt;The empty dependency array ([]) ensures this effect runs only once.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;5. Finding the Longest Name:&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj59kttpd635o6lu2q8vi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj59kttpd635o6lu2q8vi.png" alt="Image description" width="500" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This function iterates through the comments array to find and return the longest name.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;6. Using useMemo:&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4svdqhzbckotxo3u7iqw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4svdqhzbckotxo3u7iqw.png" alt="Image description" width="695" height="48"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;useMemo memoizes the result of findLongestName(data).&lt;/li&gt;
&lt;li&gt;The function findLongestName(data) is called only when toggle changes.&lt;/li&gt;
&lt;li&gt;If toggle remains unchanged, the previously computed result is returned without recomputing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;7. Rendering the Component:&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqf8gmtp6kfuejfntngdn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqf8gmtp6kfuejfntngdn.png" alt="Image description" width="365" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;getLongestName is displayed in a div.&lt;/li&gt;
&lt;li&gt;A button toggles the toggle state.&lt;/li&gt;
&lt;li&gt;When toggle is true, "toggle" is displayed in an h1.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why Use useMemo?
&lt;/h2&gt;

&lt;h4&gt;1. Performance Optimization:&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;It helps prevent expensive calculations from being performed on every render.&lt;/li&gt;
&lt;li&gt;In this example, findLongestName can be computationally expensive if the list of comments is large.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;2. Dependencies:&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The result of the memoized function (findLongestName) is recalculated only when the specified dependencies ([toggle]) change.&lt;/li&gt;
&lt;li&gt;If the dependencies do not change, React returns the memoized value from the previous render.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;3. Example Context:&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Here, useMemo ensures findLongestName(data) is only recalculated when toggle changes.&lt;/li&gt;
&lt;li&gt;Even if the component re-renders due to changes in other state or props, the memoized result is reused, avoiding redundant computations.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>React Context API</title>
      <dc:creator>Geetika Bajpai</dc:creator>
      <pubDate>Wed, 26 Jun 2024 19:19:42 +0000</pubDate>
      <link>https://dev.to/geetika_bajpai_a654bfd1e0/react-context-api-4ig8</link>
      <guid>https://dev.to/geetika_bajpai_a654bfd1e0/react-context-api-4ig8</guid>
      <description>&lt;h2&gt;
  
  
  Why do we need context API.
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwzh4izg9t2ghosddvivp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwzh4izg9t2ghosddvivp.png" alt="Image description" width="687" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In React, passing props is a fundamental concept that enables a parent component to share data with its child components as well as other components within an application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prop Drilling Explained:
&lt;/h2&gt;


&lt;h3&gt;State in Component A:&lt;/h3&gt;Component A holds a piece of state, value: 1, and a function to update it, setCount().&lt;br&gt;
&lt;h3&gt;Passing State through Components:&lt;/h3&gt;To get the state (value) and the function (setCount()) to Component D, Component A needs to pass them as props to Component B.Component B, in turn, passes these props down to Component C.Finally, Component C passes them down to Component D.

&lt;h2&gt;Problems with Prop Drilling:&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Complexity: As the number of components grows, the process of passing props through each intermediate component becomes cumbersome and error-prone.&lt;/li&gt;
&lt;li&gt;Maintenance: If the state or function needs to be used by additional components at different levels, the structure must be adjusted accordingly, leading to increased complexity and reduced maintainability.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Need for React Context API:
&lt;/h2&gt;

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

&lt;p&gt;React Context API allows for state to be managed and accessed at a centralized place inside context. With Context API, the state and functions can be made available directly to any component in the component tree without passing props through each intermediate component.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Simplified Code:&lt;/u&gt; No need to pass props through multiple layers, leading to cleaner and more understandable code.&lt;br&gt;
&lt;u&gt;Flexibility:&lt;/u&gt; State and functions can be accessed and updated by any component within the provider, making the application more flexible and easier to maintain.&lt;/p&gt;

&lt;p&gt;Now component A and D both can access without prop drilling. If component A change the state automatically component D re-render. Because component should also be re-render with fresh data. Means whenever in our context state changes those components who are reading that re-render itself with new value.&lt;/p&gt;

&lt;p&gt;If we need to give access of context to these components so you need to wrap inside contextProvider.&lt;/p&gt;

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

&lt;p&gt;if we don't give contextProvider these context will not access that context state. Now, in context these components can read and write also.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyh3leg1ke5to4a52y7o2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyh3leg1ke5to4a52y7o2.png" alt="Image description" width="530" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we will start and see the code this increment decrement.&lt;/p&gt;

&lt;p&gt;We will create &lt;code&gt;Counter.jsx&lt;/code&gt; file inside context and write this.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr6mw049rv2qtfqk202y4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr6mw049rv2qtfqk202y4.png" alt="Image description" width="800" height="356"&gt;&lt;/a&gt;&lt;br&gt;
This code defines a React context for managing a counter state and provides a context provider component to wrap parts of the application that need access to this counter state. &lt;/p&gt;

&lt;h5&gt;`export const CounterContext = createContext(null);`&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;This line creates a context using createContext from React.&lt;/li&gt;
&lt;li&gt;CounterContext will be used to share state (and potentially other values) across the component tree without having to pass props down manually at every level.&lt;/li&gt;
&lt;li&gt;Initially, the context is set to null, meaning it has no default value.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;CounterProvider&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;The useState hook to create a piece of state called count and a function to update it called setCount.&lt;/li&gt;
&lt;li&gt;count is initialized to 0.&lt;/li&gt;
&lt;li&gt;Inside returns a &lt;code&gt;CounterContext.Provider&lt;/code&gt; component the value prop of the provider is an object containing &lt;code&gt;count&lt;/code&gt;, &lt;code&gt;setCount&lt;/code&gt;, and &lt;code&gt;name: "Geet"&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;count&lt;/code&gt;: The current count value.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setCount&lt;/code&gt;: The function to update the count value.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt;: "Geet": A static value just for demonstration purposes 
 (it could be anything you want to share via context).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{props.children}&lt;/code&gt;: This ensures that any components wrapped by &lt;code&gt;CounterProvider&lt;/code&gt; will be rendered inside the provider, making the context values (count, setCount, and name) available to those components.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Wrap your application (or part of it) with &lt;code&gt;CounterProvider&lt;/code&gt; like this&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F69t7auc96cz48pcfoggf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F69t7auc96cz48pcfoggf.png" alt="Image description" width="800" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe2pms0xfs3bga1xtgkhr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe2pms0xfs3bga1xtgkhr.png" alt="Image description" width="800" height="500"&gt;&lt;/a&gt;&lt;br&gt;
This code defines a Counter component in React that interacts with a shared counter state using the CounterContext.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;useContext(CounterContext)&lt;/code&gt; is called to access the values provided by the CounterProvider.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;counterContext&lt;/code&gt; now holds the context value, which includes the count and setCount state, and any other values provided by the context.&lt;/li&gt;
&lt;li&gt;The Counter component returns a div containing two buttons.&lt;/li&gt;
&lt;li&gt;The Increment button has an onClick handler that calls setCount with the current count incremented by 1.&lt;/li&gt;
&lt;li&gt;The Decrement button has an onClick handler that calls setCount with the current count decremented by 1.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faikujcslsyt0d8ok7j7r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faikujcslsyt0d8ok7j7r.png" alt="Image description" width="707" height="673"&gt;&lt;/a&gt;&lt;br&gt;
This code defines the App component for a React application that uses the CounterContext to manage and display a shared counter state.&lt;/p&gt;

&lt;h5&gt;App Component:&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Uses useContext to access the CounterContext.&lt;/li&gt;
&lt;li&gt;Displays the current count from the context.&lt;/li&gt;
&lt;li&gt;Renders four Counter components, each of which can increment and decrement the shared counter state.&lt;/li&gt;
&lt;li&gt;This setup demonstrates how React context can be used for state management across multiple components, providing a shared state that can be accessed and modified by any component within the context provider.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>useImperativeHandle hook</title>
      <dc:creator>Geetika Bajpai</dc:creator>
      <pubDate>Tue, 25 Jun 2024 11:32:50 +0000</pubDate>
      <link>https://dev.to/geetika_bajpai_a654bfd1e0/useimperativehandle-hook-1ghn</link>
      <guid>https://dev.to/geetika_bajpai_a654bfd1e0/useimperativehandle-hook-1ghn</guid>
      <description>&lt;p&gt;&lt;u&gt;useImperativeHandle:&lt;/u&gt; This hook customizes the instance value that is exposed to parent components when using ref. It allows you to define methods on the child component that can be called from the parent component.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;forwardRef: &lt;/u&gt;This is a React higher-order component that allows a parent component to directly interact with a child component's ref. It forwards the ref through the component to one of its child DOM nodes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feguwozin23ymhxvp9ij3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feguwozin23ymhxvp9ij3.png" alt="Image description" width="751" height="628"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h3&gt;State Management:&lt;/h3&gt;useState hook is used to create a state variable toggle with an initial value of false.&lt;br&gt;
setToggle is the function to update the toggle state.


&lt;h3&gt;Forwarding the Ref:&lt;/h3&gt;The Button component is wrapped with forwardRef to forward the ref to its internal DOM or methods.


&lt;h3&gt;Using useImperativeHandle:&lt;/h3&gt;Inside useImperativeHandle, we define an object with the method alterToggle.&lt;br&gt;
alterToggle method toggles the value of toggle state.&lt;br&gt;
This method will be exposed to the parent component through the ref.


&lt;h3&gt;Rendering:&lt;/h3&gt;The component renders a button with the text "Button From Child".&lt;br&gt;
If toggle is true, it also renders a &lt;span&gt; with the text "Toggle".

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmr3jmsetzty42gt1j5n8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmr3jmsetzty42gt1j5n8.png" alt="Image description" width="417" height="630"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;Creating a Ref:&lt;/h3&gt;useRef hook is used to create a ref called buttonRef. This ref will be used to access the alterToggle method defined in the Button component.




&lt;h3&gt;Parent Button:&lt;/h3&gt;A button is rendered with the text "Button From Parent".When this button is clicked, it calls buttonRef.current.alterToggle(). This triggers the alterToggle method in the Button component, toggling the toggle state.




&lt;h3&gt;Child Button:&lt;/h3&gt;The Button component is rendered with ref={buttonRef}, which forwards the buttonRef to the Button component, making the alterToggle method accessible to the parent.

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;&lt;u&gt;forwardRef:&lt;/u&gt; Allows the parent to pass a ref to the child component.&lt;br&gt;
&lt;u&gt;useImperativeHandle:&lt;/u&gt; Exposes custom methods (alterToggle in this case) from the child component to the parent component through the ref.&lt;/p&gt;

&lt;p&gt;This pattern is useful when you need to control a child component imperatively, especially when dealing with complex or non-trivial UI interactions that are difficult to achieve through props alone.&lt;/p&gt;

&lt;/span&gt;

</description>
    </item>
    <item>
      <title>useLayoutEffect hook</title>
      <dc:creator>Geetika Bajpai</dc:creator>
      <pubDate>Tue, 25 Jun 2024 10:51:33 +0000</pubDate>
      <link>https://dev.to/geetika_bajpai_a654bfd1e0/uselayouteffect-hook-5ca2</link>
      <guid>https://dev.to/geetika_bajpai_a654bfd1e0/uselayouteffect-hook-5ca2</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjhfl314ihp4yl88cxac1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjhfl314ihp4yl88cxac1.png" alt="Image description" width="800" height="506"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;useLayoutEffect is called with a callback function and an empty dependency array ([]).&lt;/li&gt;
&lt;li&gt;This hook runs synchronously after DOM updates but before the browser paints the screen.&lt;/li&gt;
&lt;li&gt;In this example, it logs the current value of the input referenced by inputRef.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;u&gt;useLayoutEffect:&lt;/u&gt; This hook is similar to useEffect, but it fires synchronously after all DOM mutations. It is useful when you need to perform operations that require measurements or calculations involving the DOM, just before the browser paints. This can include updating styles, calculating dimensions, or querying DOM properties that affect layout. In this example, useLayoutEffect logs the initial value of the input field ("PEDRO") before any layout updates occur.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use useLayoutEffect
&lt;/h2&gt;

&lt;p&gt;&lt;u&gt;Use Case:&lt;/u&gt; You would use useLayoutEffect when you need to ensure that your code runs synchronously after DOM mutations but before the browser paints the screen. This can be critical for operations that require precise DOM measurements or visual updates that need to be reflected immediately to avoid flickering or layout shifts.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Comparison with useEffect:&lt;/u&gt; If your side effect does not need to interact with the DOM synchronously or doesn't depend on the current visual state of the component, useEffect is typically more appropriate. useEffect runs asynchronously after the browser paints, making it suitable for less time-sensitive operations.&lt;/p&gt;

&lt;p&gt;The LayoutEffectTutorial component showcases the usage of useLayoutEffect and useEffect hooks in React. useLayoutEffect is utilized for synchronously accessing and logging the initial value of an input field before layout changes, while useEffect asynchronously sets an initial value for the input field. These hooks, combined with useRef, demonstrate how to manage side effects and references in functional components effectively.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>useReducer hook</title>
      <dc:creator>Geetika Bajpai</dc:creator>
      <pubDate>Tue, 25 Jun 2024 10:07:02 +0000</pubDate>
      <link>https://dev.to/geetika_bajpai_a654bfd1e0/usereducer-hook-550i</link>
      <guid>https://dev.to/geetika_bajpai_a654bfd1e0/usereducer-hook-550i</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnpp7txjn1x23kcxsniax.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnpp7txjn1x23kcxsniax.png" alt="Image description" width="437" height="291"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will see how to implement this using the useState hook.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw72pf8r1izn0zreaj7px.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw72pf8r1izn0zreaj7px.png" alt="Image description" width="605" height="657"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The ReducerTutorial React component demonstrates the use of the useState hook to manage two state variables: count (initialized to 0) and showText (initialized to true). The component renders a count value, a button, and conditionally displays a paragraph of text based on the value of showText. When the button is clicked, the count is incremented by 1 and showText is toggled between true and false, controlling the visibility of the text.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding useReducer
&lt;/h2&gt;

&lt;p&gt;useReducer is a React hook that is used for managing more complex state logic in a component, especially when the state depends on previous states or involves multiple state variables. It is an alternative to useState and is particularly useful when you have a complex state object or need to handle complex state transitions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use useReducer Here
&lt;/h2&gt;

&lt;p&gt;In the context of the ReducerTutorial component, useReducer could be used instead of useState for several reasons:&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Complex State Logic:&lt;/u&gt; If the state transitions (how state changes in response to actions) are complex, useReducer provides a more structured approach.&lt;br&gt;
&lt;u&gt;Multiple State Variables: &lt;/u&gt;Managing multiple related state variables can become cumbersome with useState. useReducer allows grouping related state variables together.&lt;br&gt;
&lt;u&gt;Predictable State Updates:&lt;/u&gt; useReducer ensures state updates are predictable and easier to trace, as all state changes are handled through a single function (the reducer).&lt;/p&gt;

&lt;h2&gt;
  
  
  What is useReducer
&lt;/h2&gt;

&lt;p&gt;useReducer is a hook that takes two arguments:&lt;/p&gt;

&lt;p&gt;A reducer function: (state, action) =&amp;gt; newState&lt;br&gt;
An initial state&lt;br&gt;
The reducer function contains the logic to determine the new state based on the current state and the action dispatched. useReducer returns an array with the current state and a dispatch function, which is used to dispatch actions that trigger state transitions.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Code Explanation
&lt;/h2&gt;

&lt;p&gt;The ReducerTutorial component demonstrates the use of the useReducer hook to manage complex state in React. It defines a reducer function to handle state transitions based on dispatched actions. The component maintains two state variables, count and showText, initialized to 0 and true, respectively. On clicking the button, it dispatches INCREMENT to increase the count and TOGGLE_TEXT to toggle the visibility of a text paragraph. The useReducer hook provides a structured approach to state management, making it suitable for handling multiple related state variables and complex state logic.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>useEffect, useRef and useCallback with 1 project</title>
      <dc:creator>Geetika Bajpai</dc:creator>
      <pubDate>Sun, 23 Jun 2024 11:47:11 +0000</pubDate>
      <link>https://dev.to/geetika_bajpai_a654bfd1e0/useeffect-useref-and-usecallback-with-1-project-f7e</link>
      <guid>https://dev.to/geetika_bajpai_a654bfd1e0/useeffect-useref-and-usecallback-with-1-project-f7e</guid>
      <description>&lt;h2&gt;
  
  
  Password Generator
&lt;/h2&gt;

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

&lt;h3&gt;
  
  
  Thoughts
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;There is a method currently running that generates random text by default.&lt;/li&gt;
&lt;li&gt;This method needs to execute repeatedly because any changes in the input parameters, such as length, or toggling checkboxes for numbers and characters, result in new random text being generated.&lt;/li&gt;
&lt;li&gt;Since this method runs frequently, we should consider optimization techniques. We can leverage memoization, which is inherently supported by React hooks, to optimize these methods effectively.&lt;/li&gt;
&lt;li&gt;The copy button should be configured to specifically target and copy only the text within the designated text box.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Don't panic read the full article you will understand and this article is written by watching videos of Sir Hitesh Choudhary in YT and reading documentation.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a Vite project: &lt;code&gt;npm create vite@latest&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Navigate to the project directory: &lt;code&gt;cd my-vite-project&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Install &lt;code&gt;npm install -D tailwindcss autoprefixer&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Install &lt;code&gt;npx tailwindcss init -p&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Install dependencies: npm install&lt;/li&gt;
&lt;li&gt;Run the development server: npm run dev&lt;/li&gt;
&lt;li&gt;Configure &lt;code&gt;tailwind.config.js&lt;/code&gt;
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feu98p13e5548ygqs1gxl.png" alt="Image description" width="616" height="407"&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, we gonna start with code.&lt;/p&gt;

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

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

&lt;h2&gt;
  
  
  Memoization with useCallback
&lt;/h2&gt;

&lt;h3&gt;`passwordGenerator` Function&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;u&gt;Purpose:&lt;/u&gt; This function generates a random password based on the specified length and character set.&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Memoization:&lt;/u&gt; The useCallback hook memoizes the passwordGenerator function, ensuring that it only gets recreated if any of the dependencies (length, numberAllowed, charAllowed, setPassword) change. This helps in avoiding unnecessary function re-creations and optimizes performance.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;`copyPasswordToClipboard` Function&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;u&gt;Purpose:&lt;/u&gt; This function copies the generated password to the clipboard.&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Memoization: &lt;/u&gt;The useCallback hook memoizes the copyPasswordToClipboard function, ensuring it only gets recreated if the password dependency changes. This optimizes performance by preventing unnecessary re-creations of the function.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;useEffect Hook&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;u&gt;Purpose:&lt;/u&gt; This effect runs the passwordGenerator function whenever the dependencies (length, numberAllowed, charAllowed, passwordGenerator) change.&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Dependency: &lt;/u&gt;The passwordGenerator function is memoized, which means the effect will only rerun when the actual logic inside the generator needs to change, optimizing the component’s rendering.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;Explanation of Memoization&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;u&gt;Why Use Memoization?: &lt;/u&gt;Memoization helps to avoid unnecessary recalculations or re-creations of functions, especially useful in functional components where functions might be redefined on each render.&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Performance Optimization: &lt;/u&gt;By using useCallback, the component ensures that passwordGenerator and copyPasswordToClipboard are only recreated when their respective dependencies change. This can reduce the rendering overhead and improve the overall performance of the component.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;Component Functionality&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;u&gt;State Management:&lt;/u&gt; The component uses useState to manage length, numberAllowed, charAllowed, and password.&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Refs:&lt;/u&gt; The passwordRef is used to reference the password input field for selecting and copying the password.&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Event Handling: The&lt;/u&gt; component handles various user interactions like changing the password length, toggling the inclusion of numbers and special characters, and copying the password to the clipboard.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Overall, the use of useCallback in this component ensures that functions are only recreated when necessary, optimizing the performance by avoiding unnecessary re-renders and re-creations.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Why you need hooks and project</title>
      <dc:creator>Geetika Bajpai</dc:creator>
      <pubDate>Sat, 22 Jun 2024 18:18:07 +0000</pubDate>
      <link>https://dev.to/geetika_bajpai_a654bfd1e0/why-you-need-hooks-and-project-ii6</link>
      <guid>https://dev.to/geetika_bajpai_a654bfd1e0/why-you-need-hooks-and-project-ii6</guid>
      <description>&lt;ol&gt;
&lt;li&gt;Install Vite:
Open your terminal or command prompt and run the following command to create a new Vite project:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxs9i5omeewv3ojundne5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxs9i5omeewv3ojundne5.png" alt="Image description" width="282" height="42"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to Your Project Directory:&lt;br&gt;
Change directory to your newly created project directory by using command cd.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install Dependencies:&lt;br&gt;
Once inside the project directory, install the necessary dependencies by running: &lt;code&gt;npm install&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the Development Server:&lt;br&gt;
Start the development server to see your project in action: &lt;code&gt;npm run dev&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We will explore hooks by creating a project and understanding the problems we solve.&lt;/p&gt;

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

&lt;p&gt;The addValue function is defined to increment the counter variable. It logs the current counter value to the console and then increments it by 1.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding problem
&lt;/h2&gt;

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

&lt;h3&gt;Issues and Improvements&lt;/h3&gt;

&lt;p&gt;Like we use one variable to more places if we do these things in classic javascript it create many references like document.getElementById, document.getElementByClassname, change the text inside the button. So, in react to change in UI react provide hooks&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;u&gt;State Management:&lt;/u&gt; The counter should be a stateful variable to ensure that changes are reflected in the UI. This can be achieved using the useState hook.&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Reactivity:&lt;/u&gt; To make the component re-render on counter changes, the counter variable should be managed by React's state.&lt;/li&gt;
&lt;/ol&gt;

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

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

&lt;p&gt;The useState hook is a function that takes an initial state as an argument and returns an array with two elements: the current state value and a function that allows you to update that value.&lt;br&gt;
•  useState Hook: const [counter, setCounter] = useState(0); initializes a state variable counter with an initial value of 0 and provides a function setCounter to update it.&lt;br&gt;
•  State Update: setCounter(counter + 1); updates the state, causing the component to re-render and display the updated counter value.&lt;/p&gt;

&lt;h3&gt;Here it is another example of useState&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm8wkw38ovs86e710einh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm8wkw38ovs86e710einh.png" alt="Image description" width="435" height="47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzh0p1z84k5b624bc2y5v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzh0p1z84k5b624bc2y5v.png" alt="Image description" width="606" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>React Router | React Router DOM</title>
      <dc:creator>Geetika Bajpai</dc:creator>
      <pubDate>Fri, 21 Jun 2024 21:33:00 +0000</pubDate>
      <link>https://dev.to/geetika_bajpai_a654bfd1e0/react-router-react-router-dom-1blo</link>
      <guid>https://dev.to/geetika_bajpai_a654bfd1e0/react-router-react-router-dom-1blo</guid>
      <description>&lt;h2&gt;
  
  
  What is React Router?
&lt;/h2&gt;

&lt;p&gt;Traditional multi-page web apps have multiple view files for rendering different views, while modern Single Page Applications (SPAs) use component-based views. This necessitates switching components based on the URL, which is handled via routing. Although not all development requirements in React need a third-party library, routing is complex and requires a pre-developed solution for efficient app development.&lt;/p&gt;

&lt;p&gt;React Router is the most popular and fully-featured routing library for React SPAs. It is lightweight, has an easy-to-learn API, and offers well-written documentation, making it a go-to choice for implementing routing in React apps. Now maintained by Remix, React Router benefits from active development and support.&lt;/p&gt;

&lt;p&gt;Unlike traditional websites where routing requests are sent to the server, React Router allows SPAs to handle all routing on the frontend, eliminating the need for additional server requests for new pages. Routing enables seamless navigation between different parts of an application when a user enters a URL or interacts with elements like links or buttons, thus playing a crucial role in building responsive and user-friendly web applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Case of React Router
&lt;/h2&gt;

&lt;p&gt;To create a React application using create-react-app, go to your preferred CMD and type:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnqf157qerwl2w1smof7y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnqf157qerwl2w1smof7y.png" alt="Image description" width="456" height="35"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The command will create a React application called router-tutorial. Once the application has been successfully created, switch to the app directory using cd router-tutorial in your code editor terminal.&lt;/p&gt;

&lt;p&gt;To use React Router in your application, you need to install it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz3e5adhvcmuzl50vrwgf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz3e5adhvcmuzl50vrwgf.png" alt="Image description" width="418" height="57"&gt;&lt;/a&gt;&lt;br&gt;
run the command below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6pr3zxjm38i9448oeb5f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6pr3zxjm38i9448oeb5f.png" alt="Image description" width="212" height="57"&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create ReactApp
&lt;/h2&gt;

&lt;p&gt;Once your project is set up and React Router is installed as a dependency, open the &lt;code&gt;src/index.js&lt;/code&gt; in your text editor. Import &lt;code&gt;BrowserRouter&lt;/code&gt; from &lt;code&gt;react-router-dom&lt;/code&gt; near the top of your file and wrap your app in a &lt;code&gt;&amp;lt;BrowserRouter&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkgpwzrxnraqd1gjpwiah.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkgpwzrxnraqd1gjpwiah.png" alt="Image description" width="422" height="330"&gt;&lt;/a&gt;&lt;br&gt;
Wrapping &lt;code&gt;&amp;lt;App /&amp;gt;&lt;/code&gt; with &lt;code&gt;&amp;lt;BrowserRouter&amp;gt;&lt;/code&gt; will apply the React Router to the components where routing is needed. That is, if you want to route in your application, you have to wrap the &lt;code&gt;&amp;lt;App /&amp;gt;&lt;/code&gt; application with &lt;code&gt;&amp;lt;BrowserRouter&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;BrowserRouter: For React Router to work, it has to be aware and in control of your application’s location. The &lt;code&gt;&amp;lt;BrowserRouter&amp;gt;&lt;/code&gt; component makes that possible when you wrap the entire application within it. Wrapping the entire application with &lt;code&gt;&amp;lt;BrowserRouter&amp;gt;&lt;/code&gt; ensures the use of routes within it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Rendering Routes
&lt;/h2&gt;

&lt;p&gt;Now that we have successfully set up React Router, to implement routing in our application, we have to render a route (that is, all the pages or components we want to navigate in our browser). To render our routes, we will set up a route for every component in our application.&lt;/p&gt;

&lt;p&gt;Routes: Whenever we have more than one route, we wrap them up in Routes. If the application’s location changes, Routes look through all its child Route to find the best match and render that branch of UI.&lt;/p&gt;

&lt;p&gt;Route: Route(s) are objects passed to the router creation function. Route renders a component whenever a user navigates to the component path.&lt;/p&gt;

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

&lt;p&gt;This code sets up a basic React application with routing using React Router. It defines two functional components, Home and About, which display simple messages. The BrowserRouter component enables routing, and the Routes and Route components define the paths for the home and about pages. When a user navigates to the root path '/', the Home component is displayed, and when they navigate to '/about', the About component is displayed. This setup provides the structure for a single-page application (SPA) where navigation between different parts of the app is handled on the client side without full page reloads.&lt;/p&gt;

&lt;h2&gt;
  
  
  Multilevel routes
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fml4lpbz7suuvyx5rcb7h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fml4lpbz7suuvyx5rcb7h.png" alt="Image description" width="742" height="525"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Another method to achieve nested routes
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyxx7sh9ncu5expc63qx4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyxx7sh9ncu5expc63qx4.png" alt="Image description" width="752" height="820"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Nested Routes:&lt;u&gt;&lt;/u&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;Parent Route (Account):The Route with path 'account' does not have an element prop, indicating it is used to group child routes.This route serves as a container for the nested routes.&lt;/p&gt;

&lt;p&gt;Child Routes: Nested routes under 'account':&lt;br&gt;
'/account/profile' renders the Profile component.&lt;br&gt;
'/account/settings' renders the Settings component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dynamic Route
&lt;/h2&gt;

&lt;p&gt;The e-commerce site uses dynamic routing to handle different categories and specific products. Similar to user-based content sites like blogs or social media, each route has a common structure but displays different content based on the URL parameters. This approach helps in organizing and displaying content efficiently, making the site easy to navigate and manage. This is what's known as dynamic pages and routes.&lt;/p&gt;

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

&lt;p&gt;useParams hook is used to access the URL parameters. In this case, it captures the userName parameter from the URL.&lt;/p&gt;

&lt;p&gt;It defines a route with a dynamic segment (:userName), captures the parameter using the useParams hook, and uses it within a component to display personalized content. This approach is useful for scenarios where the content or component behavior needs to change based on URL parameters, such as user profiles, product details, or other similar dynamic pages.&lt;/p&gt;

&lt;p&gt;Now let's go ahead and add in our API call.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3n5sl84sdfsb6nr3ks1d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3n5sl84sdfsb6nr3ks1d.png" alt="Image description" width="800" height="451"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb1lc689dujlo9452hvmv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb1lc689dujlo9452hvmv.png" alt="Image description" width="800" height="713"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Home Component&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;State Management: Uses the useState hook to store the list of posts.&lt;/li&gt;
&lt;li&gt;Data Fetching: The useEffect hook fetches data from jsonplaceholder.typicode.com/posts when the component mounts and sets the posts state.&lt;/li&gt;
&lt;li&gt;Rendering Posts: Maps over the posts array to create a list of links (NavLink) that navigate to a detailed view of each post. The to prop of NavLink uses a dynamic URL (/post/${post.id}).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;PostPage Component&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;useParams: Retrieves the postId from the URL.&lt;/li&gt;
&lt;li&gt;State Management: Uses the useState hook to store the fetched post data.&lt;/li&gt;
&lt;li&gt;Data Fetching: The useEffect hook fetches data for the specific post ID (params.postId) whenever params.postId changes.&lt;/li&gt;
&lt;li&gt;Conditional Rendering: Displays a loading message until the data is fetched and then displays the post’s title and body.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;Summary&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Home Component: Fetches and displays a list of posts. Each post is a link that navigates to a detailed view of the post.&lt;/li&gt;
&lt;li&gt;PostPage Component: Fetches and displays details of a specific post based on the postId URL parameter.&lt;/li&gt;
&lt;li&gt;React Router: Handles navigation between the home page and the post detail page.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>React Lifecycle Methods Using Class &amp; Functional Components</title>
      <dc:creator>Geetika Bajpai</dc:creator>
      <pubDate>Fri, 21 Jun 2024 14:56:36 +0000</pubDate>
      <link>https://dev.to/geetika_bajpai_a654bfd1e0/react-lifecycle-methods-using-class-functional-components-1899</link>
      <guid>https://dev.to/geetika_bajpai_a654bfd1e0/react-lifecycle-methods-using-class-functional-components-1899</guid>
      <description>&lt;p&gt;Components in React serve as the fundamental building blocks of an application. Essentially, a React component returns a JSX element that is rendered in the user interface. Additionally, a component can encompass other features such as state management, properties (props), and lifecycle methods.&lt;/p&gt;

&lt;p&gt;React categorizes components into two main types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Functional components: These are JavaScript functions that return JSX elements. Initially, they were stateless components, lacking support for managing state. However, with the introduction of React hooks in version 16.8, functional components gained the ability to declare and manage state, transforming them into stateful components.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo1i6ewlv7wom6kuxcrpd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo1i6ewlv7wom6kuxcrpd.png" alt="Image description" width="311" height="216"&gt;&lt;/a&gt;&lt;br&gt;
In the above code, SayHello is a functional component and it is returning a JSX element. In the end, the SayHello function is being exported. Basically, SayHello is nothing but a plain JavaScript function. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Class components: These are JavaScript classes extended to React.Component. They require a mandatory render() method that returns a JSX element. Historically, React heavily relied on class components because only they supported local state management ("state").&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbghkyi0884otgoq4ds0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbghkyi0884otgoq4ds0.png" alt="Image description" width="486" height="230"&gt;&lt;/a&gt;&lt;br&gt;
The above class component is the equivalent of the earlier functional component. It is easily observable that the functional component is more easily readable than the class component. Moreover, it is easier to work with functional components because writing class components is more complex and complicated. &lt;/p&gt;

&lt;h1&gt;
  
  
  Class Component
&lt;/h1&gt;

&lt;p&gt;These are mounting, updating, and unmounting. The lifecycle methods are very useful because sometimes, we may want to execute a piece of code at a specific time. For example, suppose we want to call an API just after the component is mounted. In such a case, we can use the componentDidMount lifecycle method. &lt;/p&gt;

&lt;h2&gt;
  
  
  Mounting phase with componentDidMount
&lt;/h2&gt;

&lt;p&gt;This is the first stage of a React component’s lifecycle where the component is created and inserted into the DOM.The componentDidMount lifecycle method executes once after initial rendering. If an empty dependency array is provided as the second argument, the callback function executes only after initial rendering.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Analyze variables before loading using the constructor.
&lt;/h2&gt;

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

&lt;p&gt;1.Constructor (constructor()):The &lt;code&gt;constructor()&lt;/code&gt; method is a special method for initializing state (this.state) and binding event handler methods. In this code, super() is called to invoke the constructor of the parent Component class (from which App extends). Then, this.state is initialized with an object containing count: 0.&lt;/p&gt;

&lt;p&gt;2.Component Did Mount (componentDidMount()):&lt;code&gt;componentDidMount()&lt;/code&gt; is a lifecycle method that is called immediately after a component is mounted (i.e., inserted into the DOM tree). In this code, it logs a message to the console indicating that the component has rendered for the first time. This is useful for performing actions that require DOM nodes or initial state setup.&lt;/p&gt;

&lt;p&gt;3.Increment Method (increment()):&lt;code&gt;increment()&lt;/code&gt; is a custom method defined within the App class. When called, it updates the component's state using &lt;code&gt;this.setState()&lt;/code&gt;. In React, &lt;code&gt;setState()&lt;/code&gt; is used to update state values asynchronously. Here, it increments the count state by 1 each time the button is clicked.&lt;/p&gt;

&lt;p&gt;4.Component Will Unmount (componentWillUnmount()):This lifecycle method is called just before the component is removed from the DOM. It's useful for cleanup activities such as cancelling network requests, removing event listeners, or cleaning up subscriptions. Here, it logs a message indicating that the component is being removed.&lt;/p&gt;

&lt;p&gt;5.Render Method (render()):The &lt;code&gt;render()&lt;/code&gt; method is a required method in every React component class. It returns JSX (JavaScript XML) that defines the UI of the component. In this code, it renders a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; containing an &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; element displaying the current value of &lt;code&gt;this.state.count&lt;/code&gt; and a &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; element that calls &lt;code&gt;this.increment()&lt;/code&gt; when clicked.&lt;/p&gt;

&lt;p&gt;Binding Methods:&lt;br&gt;
•Method 1:Arrow Function in Render Method &lt;code&gt;(onClick={() =&amp;gt; { this.increment() }})&lt;/code&gt;:This approach creates a new function instance every time the component renders. While straightforward, this can lead to performance implications, especially in larger applications.&lt;/p&gt;

&lt;p&gt;•Method 2:Binding in Constructor &lt;code&gt;(onClick={this.increment.bind(this)})&lt;/code&gt;:&lt;br&gt;
This approach binds this.increment() to the current instance of App in the constructor. It ensures that this refers to the component instance when increment() is called, avoiding the need for creating new function instances on each render.&lt;/p&gt;

&lt;p&gt;Lifecycle Summary:&lt;br&gt;
•Mounting Phase:During the mounting phase, React calls the constructor() first, initializing state. Then, render() is called, returning JSX to render the component's UI. Finally, componentDidMount() is invoked after the component is rendered for the first time, where initialization tasks, API calls, or subscriptions can be performed.&lt;/p&gt;

&lt;p&gt;•State Management:this.state allows components to manage their own state. Updates to state using this.setState() trigger re-rendering of the component, ensuring UI remains synchronized with data changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Updating phase with componentDidUpdate
&lt;/h2&gt;

&lt;p&gt;This updating stage occurs after the component has mounted and rendered into the DOM. A React component updates when there is a change in its props or state. The componentDidUpdate lifecycle method is invoked after the component updates. This method can be used to compare if a specific prop or state has changed.&lt;/p&gt;

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

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Component Did Update &lt;code&gt;(componentDidUpdate(prevProps, prevState))&lt;/code&gt;:&lt;br&gt;
componentDidUpdate() is a lifecycle method invoked immediately after a component updates and after render() is called. It receives two arguments:&lt;br&gt;
o prevProps: Represents the previous props before the update.&lt;br&gt;
o prevState: Represents the previous state before the update.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;if (prevProps.number !== this.props.number) { console.log("Component Updated"); }&lt;/code&gt;: Compares the current number prop &lt;code&gt;(this.props.number)&lt;/code&gt; with its previous value &lt;code&gt;(prevProps.number)&lt;/code&gt;. If they differ, it logs "Component Updated" to indicate that the component has been updated due to a change in props.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Updating Phase:&lt;br&gt;
• During the updating phase, React compares the previous props and state with the current props and state to determine if the component needs to re-render. componentDidUpdate() is called after this comparison and rendering process.&lt;br&gt;
• It is typically used for operations that need to be performed after a component updates, such as fetching new data based on props changes or interacting with the DOM.&lt;/p&gt;

&lt;h2&gt;
  
  
  Now, let's see how this can be achieved in a functional component.
&lt;/h2&gt;

&lt;p&gt;The useEffect hook is used to perform side effects in functional components. It takes two arguments: a callback function and a dependency array. The callback function executes based on the presence and values in the dependency array. This is how lifecycle method-like functionality is achieved in functional components.&lt;/p&gt;

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

&lt;p&gt;• Cleanup Function: The function returned from the useEffect hook is the cleanup function. It runs when the component is unmounted or before the effect is re-executed due to dependency changes. Here, it logs "Functional Component: Removed" to the console.&lt;/p&gt;

&lt;p&gt;• Dependency Array: The second argument to useEffect is the dependency array. This array specifies the dependencies that the effect depends on. If any value in this array changes, the effect will re-run. In this case, [number] indicates that the effect should re-run whenever the number prop changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lifecycle Summary in the Context of useEffect:
&lt;/h2&gt;

&lt;p&gt;• Mounting Phase: When the Counter1 component is first rendered, useEffect runs and logs "Functional Component: Updating..." to the console.&lt;/p&gt;

&lt;p&gt;• Updating Phase: Whenever the number prop changes, useEffect runs again. Before running the effect function, React calls the cleanup function from the previous effect, logging "Functional Component: Removed" and then logs "Functional Component: Updating..." again.&lt;/p&gt;

&lt;p&gt;• Unmounting Phase: When the Counter1 component is removed from the DOM, the cleanup function from the last effect run is called, logging "Functional Component: Removed" to the console.&lt;/p&gt;

&lt;h2&gt;
  
  
  State in functional components
&lt;/h2&gt;

&lt;p&gt;Initially, it was not possible to use state in functional components because the setState() method was only supported in class components. However, with the introduction of React hooks, it is now possible to use state in functional components.&lt;/p&gt;

&lt;p&gt;The state of a component in React is a plain JavaScript object that controls the behavior of the component. A change in state triggers a re-render of the component.&lt;/p&gt;

&lt;p&gt;There are two main React hooks used to declare and manipulate the state in a functional component:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;useState&lt;/li&gt;
&lt;li&gt;useReducer&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;The Counter functional component in React uses the useState hook to manage a count state variable. The useState(0) hook initializes count to 0 and provides a setCounter function to update it. The component returns a fragment containing two buttons and an &lt;code&gt;&amp;lt;h2&amp;gt;&lt;/code&gt; element. The first button increments the count by 1, and the second button decrements the count by 1, both using the setCounter function. The current count value is displayed inside the &lt;code&gt;&amp;lt;h2&amp;gt;&lt;/code&gt; element. This demonstrates how to handle state and update it in response to user actions using the useState hook in a functional component.&lt;/p&gt;

&lt;p&gt;The useReducer hook is used for complex state management. When the state is interdependent, the useReducer hook is preferred over the useState hook. To understand useReducer, one should know what a reducer is and how to use it.&lt;/p&gt;

&lt;p&gt;A reducer is essentially a pure function that takes the current state and an action as arguments and returns a new state.&lt;/p&gt;

&lt;p&gt;It is very similar to the useState hook but instead of creating a function to update the state, the useReducer hook creates a function for dispatching actions. The useReducer hook has two arguments - reducer and an initialState. There is a third optional argument for initializing the state lazily.&lt;/p&gt;

&lt;p&gt;Following is the syntax of the useReducer hook.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5n9g2w3d8usihx6dbti9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5n9g2w3d8usihx6dbti9.png" alt="Image description" width="717" height="37"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
