<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Gakii</title>
    <description>The latest articles on Forem by Gakii (@gakii).</description>
    <link>https://forem.com/gakii</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%2F1158654%2Fd33a558a-0cee-40c2-815c-0aea3ca9f0bc.jpg</url>
      <title>Forem: Gakii</title>
      <link>https://forem.com/gakii</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/gakii"/>
    <language>en</language>
    <item>
      <title>React Router: Navigating Between Pages in Single-Page Applications</title>
      <dc:creator>Gakii</dc:creator>
      <pubDate>Sat, 09 Mar 2024 10:25:10 +0000</pubDate>
      <link>https://forem.com/gakii/react-router-navigating-between-pages-in-single-page-applications-7fb</link>
      <guid>https://forem.com/gakii/react-router-navigating-between-pages-in-single-page-applications-7fb</guid>
      <description>&lt;p&gt;A Single-Page Application (SPA), is a web application that operates within a single HTML page. It dynamically updates its content without requiring a full page reload. This is  achieved  by loading the initial HTML, CSS, and JavaScript resources and then dynamically fetching data and updating the DOM as users interact with the application. By loading and updating only the necessary data, it provides a fluid and more responsive user experience.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;React Router&lt;/code&gt; is a JavaScript library used in React applications to handle routing and navigation. It provides a declarative way to define the routes of an application and render different components based on the current URL. &lt;/p&gt;

&lt;p&gt;React Router allows developers to create a seamless, client-side navigation experience within a Single-Page Application by mapping URLs to specific components and managing the history and URL changes.&lt;/p&gt;

&lt;p&gt;First thing to do is create a Browser Router and configure our first route. This will enable client side routing for our web app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://reactrouter.com/en/main/routers/create-browser-router"&gt;createBrowserRouter&lt;/a&gt;  is the recommended router for all React Router web projects. It uses the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/History"&gt;DOM History API&lt;/a&gt; to update the URL and manage the history stack. It also enables the v6.4 data APIs like &lt;code&gt;loaders&lt;/code&gt;, &lt;code&gt;actions&lt;/code&gt;, &lt;code&gt;fetchers&lt;/code&gt; and more.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Router
&lt;/h2&gt;

&lt;p&gt;To start, create and render the browser router in the entry point file of your Application, App.js in our case. We import &lt;code&gt;createBrowserRouter&lt;/code&gt; and &lt;code&gt;RouterProvider&lt;/code&gt;  from &lt;code&gt;react-router-dom&lt;/code&gt;, and set it equal to a variable, in our case router.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Const&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createBrowserRouter&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;

&lt;span class="p"&gt;])&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;createBrowserRouter&lt;/code&gt; we pass in an array of route &lt;br&gt;
 objects. &lt;/p&gt;

&lt;p&gt;In the code example below we have a router with two pages. The home page that displays the home page element, and an about page that displays the about page element. This allows you to navigate between the home page and the about page.&lt;/p&gt;

&lt;p&gt;App.jsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createBrowserRouter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;RouterProvider&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-router-dom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createBrowserRouter&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;home&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;,
&lt;/span&gt;  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/about&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;about&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;


&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;RouterProvider&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To render the router in our Application, we return the RouterProvider component. The component has a router prop where we pass in our router variable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nested Routes
&lt;/h2&gt;

&lt;p&gt;The first route is often called the &lt;code&gt;root route&lt;/code&gt; because the rest of the routes will render inside of it. It serves as the root layout of the user interface.&lt;/p&gt;

&lt;p&gt;In the code example below we want the &lt;code&gt;&amp;lt;Landing&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;Newsletter&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;About&amp;gt;&lt;/code&gt;  components to render inside the &lt;code&gt;&amp;lt;HomeLayout&amp;gt;&lt;/code&gt; which is set as the root route's component. We achieve this by making the routes, children of the root route.&lt;/p&gt;

&lt;p&gt;We also need to set up which page will be the index page. This is the page that will be displayed when you move to the home page, and is going to match the root route URL.&lt;/p&gt;

&lt;p&gt;In the code example below the &lt;code&gt;&amp;lt;Landing&amp;gt;&lt;/code&gt; component is set as the &lt;code&gt;index page&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;App.jsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createBrowserRouter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;RouterProvider&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-router-dom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createBrowserRouter&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HomeLayout&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Landing&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;newsletter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Newsletter&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;about&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;About&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;RouterProvider&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;We then need to tell the root route where to render its child routes. Inside the root component, we import and render &lt;code&gt;&amp;lt;Outlet&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The layout on the root component is then shared across all the children nested inside it. If we have a navbar component as illustrated below, all the nested children will share this navbar component.&lt;/p&gt;

&lt;p&gt;HomeLayout.jsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Outlet&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-router-dom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Navbar&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../components/Navbar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;HomeLayout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Navbar&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;section&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Outlet&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/section&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;HomeLayout&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is recommended that all web projects use &lt;code&gt;createBrowserRouter&lt;/code&gt; as it uses the full URL instead of the hash urls common in web apps before &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/History/pushState"&gt;history.pushState&lt;/a&gt; was standardized. Full URLs are better for SEO, better for server rendering, and are just more compatible with the rest of the web platform.&lt;/p&gt;

&lt;p&gt;While your app may only use a single router, several routers are available depending on the environment your app is running in. &lt;a href="https://reactrouter.com/en/main/routers/picking-a-router"&gt;Picking a Router&lt;/a&gt; should help you figure out which one to use.&lt;/p&gt;

&lt;p&gt;Thank you for following along with me in this learning process. Follow me and leave comments, added observations or questions below.&lt;/p&gt;

</description>
      <category>reactrouter</category>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Functional State Update in React.</title>
      <dc:creator>Gakii</dc:creator>
      <pubDate>Wed, 13 Sep 2023 09:44:25 +0000</pubDate>
      <link>https://forem.com/gakii/functional-state-update-in-react-42io</link>
      <guid>https://forem.com/gakii/functional-state-update-in-react-42io</guid>
      <description>&lt;p&gt;State updates in React are &lt;strong&gt;asynchronous&lt;/strong&gt; which means that the state does not necessarily update instantly.&lt;/p&gt;

&lt;p&gt;This is by design to improve the performance of React applications. As of &lt;em&gt;React 18&lt;/em&gt;, React now batches state updates even if they were inside an event handler or a promise callback.&lt;/p&gt;

&lt;p&gt;When you update the state in React, this will require re-rendering of your component (and potentially other components), which could be an expensive operation. &lt;/p&gt;

&lt;p&gt;This is why React batches several state updates together and combines them into one re-rendering in order to make your app more responsive and reduce the amount of work the browser has to do.&lt;/p&gt;

&lt;p&gt;Let us look at an example:&lt;br&gt;
&lt;/p&gt;

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

 function App() {    
    const [date, setDate] = useState(new Date());
    const [counter, setCounter] = useState(0);

    console.log("rendered"); // to visualise re-renders

    function handleButtonClick() {
        setDate(new Date());
        setCounter(counter + 1);
    }

    return &amp;lt;button onClick={handleButtonClick}&amp;gt;Click me&amp;lt;/button&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are two state updates when the button is clicked. However, the component will only re-render once.&lt;/p&gt;

&lt;p&gt;This is because React batches (merges) these two state changes and performs them at the same time. This makes your app respond faster to user interactions.&lt;/p&gt;

&lt;p&gt;Because state updates are asynchronous, there's a caveat that we have to be aware of. For the sake of simplicity, let's say that we have the following component:&lt;br&gt;
&lt;/p&gt;

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

function App() {
    const [counter, setCounter] = useState(0);

    function handleButtonClick() {
        setCounter(counter + 1);
        setCounter(counter + 1);
    }

    return &amp;lt;button onClick={handleButtonClick}&amp;gt;Click me {counter}&amp;lt;/button&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After clicking the button once, the value of the counter will be 1 not 2. &lt;br&gt;
The two state updates will be batched together, and starting with &lt;strong&gt;counter = 0&lt;/strong&gt;, the first &lt;strong&gt;setCounter()&lt;/strong&gt; call will set the counter to &lt;strong&gt;0 + 1 = 1&lt;/strong&gt;, but not immediately because it's asynchronous.&lt;/p&gt;

&lt;p&gt;Then we have the following call &lt;strong&gt;setCounter(counter + 1)&lt;/strong&gt;, but the value of counter is still 0 because the component did not re-render yet.&lt;br&gt;
So the second call will also set the state to 1.&lt;/p&gt;
&lt;h2&gt;
  
  
  Functional state updates
&lt;/h2&gt;

&lt;p&gt;To solve this issue, React has the concept of functional state updates which is _when you pass a function to the state setter function. _&lt;/p&gt;

&lt;p&gt;The function passed to setState receives the previous state as an argument that is used to compute the next state.&lt;br&gt;
Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setCounter((previousCounter) =&amp;gt; {
    return previousCounter + 1;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use a shorter version of course:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setCounter(previousCounter =&amp;gt; previousCounter + 1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We give it a function definition that takes the previous value of the state and returns the new state. In this example, the new state is the previous one + 1.&lt;/p&gt;

&lt;p&gt;Here's how you can fix the above example to add to the state twice:&lt;br&gt;
&lt;/p&gt;

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

function App() {
    const [counter, setCounter] = useState(0);

    function handleButtonClick() {
        setCounter(prevCounter =&amp;gt; prevCounter + 1);
        setCounter(prevCounter =&amp;gt; prevCounter + 1);
    }

    return &amp;lt;button onClick={handleButtonClick}&amp;gt;Click me {counter}&amp;lt;/button&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will add 2 to the counter every time the button is clicked.&lt;br&gt;
 &lt;strong&gt;prevCounter =&amp;gt; prevCounter + 1&lt;/strong&gt; is a function definition. &lt;br&gt;
React will call this function and pass the previous value of the state as the first argument.&lt;/p&gt;

&lt;p&gt;This means that you can name that argument whatever you choose. In this example, we used &lt;em&gt;prevCounter&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Whenever the new state is computed using the previous state, it is recommended that you use functional state updates to guarantee consistency and prevent unexpected bugs. &lt;/p&gt;

&lt;p&gt;If for example you are incrementing a counter, you should use a functional state update. On the other hand, if you're resetting a counter back to 0, you don't necessarily have to use a functional state update (as the new value is not computed using the previous state).&lt;/p&gt;

&lt;p&gt;I hope this post was helpful. Please share and leave a comment if anything resonates with you.&lt;/p&gt;

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