<?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: k.Jyothi Prakash Reddy</title>
    <description>The latest articles on Forem by k.Jyothi Prakash Reddy (@jyothiprakashk).</description>
    <link>https://forem.com/jyothiprakashk</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%2F204418%2F3b389d94-bf77-4506-be62-c8e8daf11689.jpeg</url>
      <title>Forem: k.Jyothi Prakash Reddy</title>
      <link>https://forem.com/jyothiprakashk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jyothiprakashk"/>
    <language>en</language>
    <item>
      <title>React Router Migration From V5 to V6: A Comprehensive Guide 🚀</title>
      <dc:creator>k.Jyothi Prakash Reddy</dc:creator>
      <pubDate>Wed, 27 Dec 2023 06:36:55 +0000</pubDate>
      <link>https://forem.com/jyothiprakashk/react-router-migration-from-v5-to-v6-a-comprehensive-guide-4imh</link>
      <guid>https://forem.com/jyothiprakashk/react-router-migration-from-v5-to-v6-a-comprehensive-guide-4imh</guid>
      <description>&lt;p&gt;The react-router-dom-v5-compat package enables React Router web apps to incrementally migrate to the latest API in v6 by running it in parallel with v5. It is a copy of v6 with an extra couple of components to keep the two in sync&lt;/p&gt;

&lt;h3&gt;
  
  
  Incremental Migration
&lt;/h3&gt;

&lt;p&gt;Instead of upgrading and updating all of our code at once (which is incredibly difficult and may cause bugs), we planned to release the react-router migration in two phases... &lt;/p&gt;

&lt;p&gt;It looks something like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setup the &lt;code&gt;CompatRouter&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Change a &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt; inside of a &lt;code&gt;&amp;lt;Switch&amp;gt;&lt;/code&gt; to a &lt;code&gt;&amp;lt;CompatRoute&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Update all APIs inside this route element tree to v6 APIs one at a time&lt;/li&gt;
&lt;li&gt;Repeat for all routes in the &lt;code&gt;&amp;lt;Switch&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Convert the &lt;code&gt;&amp;lt;Switch&amp;gt;&lt;/code&gt; to a &lt;code&gt;&amp;lt;Routes&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Repeat for all ancestor &lt;code&gt;&amp;lt;Switch&amp;gt;&lt;/code&gt;s&lt;/li&gt;
&lt;li&gt;Update &lt;code&gt;&amp;lt;Links&amp;gt;&lt;/code&gt;
You're done!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Phase 1: 📈
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react-router-dom-v5-compat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install a Compatibility Package that is  react-router-dom-v5-compat - This package includes the v6 API so that you can run it in parallel with v5. (This package helps migrate our code stages instead of making a big, and risky upgrade in our app )&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { BrowserRouter } from "react-router-dom";
import { CompatRouter } from "react-router-dom-v5-compat";

 export function App() {
   return (
     &amp;lt;BrowserRouter&amp;gt;
      &amp;lt;CompatRouter&amp;gt;
         &amp;lt;Switch&amp;gt;
           &amp;lt;Route path="/" exact component={Home} /&amp;gt;
           {/* ... */}
         &amp;lt;/Switch&amp;gt;
      &amp;lt;/CompatRouter&amp;gt;
     &amp;lt;/BrowserRouter&amp;gt;
   );
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CompatRouter that synchronizes the v5 and v6 APIs state so that both APIs are available.&lt;/p&gt;

&lt;p&gt;Done 💥... We ready for migration &lt;/p&gt;

&lt;h2&gt;
  
  
  Phase 2:🗺️
&lt;/h2&gt;

&lt;p&gt;Convert &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt; to &lt;code&gt;&amp;lt;CompatRoute&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; import { Route } from "react-router-dom";
 import { CompatRoute } from "react-router-dom-v5-compat";

  export function SomComp() {
    return (
      &amp;lt;Switch&amp;gt;
       &amp;lt;Route path="/project/:id" component={Project} /&amp;gt;
       &amp;lt;CompatRoute path="/project/:id" component={Project} /&amp;gt;
      &amp;lt;/Switch&amp;gt;
    )
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;&amp;lt;CompatRoute&amp;gt;&lt;/code&gt; renders a v5 &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt; wrapped inside of a v6 context. This is the special sauce that makes both APIs available to the component tree inside of this route.&lt;/p&gt;

&lt;p&gt;then Migrate below React Router APIs&lt;/p&gt;

&lt;h2&gt;
  
  
  Upgrade all  elements to :
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  return (
    &amp;lt;BrowserRouter&amp;gt;  
      &amp;lt;Switch&amp;gt;
        &amp;lt;Route exact path="/"&amp;gt;
          &amp;lt;Home /&amp;gt;
        &amp;lt;/Route&amp;gt;
        &amp;lt;Route path="/products"&amp;gt;
          &amp;lt;Products /&amp;gt;
        &amp;lt;/Route&amp;gt;
      &amp;lt;/Switch&amp;gt;
 &amp;lt;/BrowserRouter&amp;gt;   
)}

function Products() {
  let match = useRouteMatch();
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;Link to={`${match.url}/me`}&amp;gt;My Profile&amp;lt;/Link&amp;gt;
      &amp;lt;Switch&amp;gt;
        &amp;lt;Route path={`${match.path}/me`}&amp;gt;
          &amp;lt;Own /&amp;gt;
        &amp;lt;/Route&amp;gt;
        &amp;lt;Route path={`${match.path}/:id`}&amp;gt;
          &amp;lt;UserProfile /&amp;gt;
        &amp;lt;/Route&amp;gt;
      &amp;lt;/Switch&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to (v6) :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  return (
    &amp;lt;BrowserRouter&amp;gt;   
  &amp;lt;Routes&amp;gt;
        &amp;lt;Route path="/" element={&amp;lt;Home /&amp;gt;} /&amp;gt;
        &amp;lt;Route path="users/*" element={&amp;lt;Products /&amp;gt;} /&amp;gt;  
   &amp;lt;/Routes&amp;gt;
    &amp;lt;/BrowserRouter&amp;gt;   
)}


function Products() {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;nav&amp;gt;
        &amp;lt;Link to="me"&amp;gt;My Profile&amp;lt;/Link&amp;gt;
      &amp;lt;/nav&amp;gt;

      &amp;lt;Routes&amp;gt;
        &amp;lt;Route path=":id" element={&amp;lt;Product /&amp;gt;} /&amp;gt;
        &amp;lt;Route path="me" element={&amp;lt;OwnUserProfile /&amp;gt;} /&amp;gt;
      &amp;lt;/Routes&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

We don’t use Component as a child anymore instead we will use Component in the element props

### what are the changes? ✅
* change all the `&amp;lt;Switch&amp;gt;` to `&amp;lt;Routes&amp;gt;`
* All `&amp;lt;Route&amp;gt;`s and `&amp;lt;Link&amp;gt;`s inside a `&amp;lt;Routes&amp;gt;` are relative. This leads to leaner and more predictable code in `&amp;lt;Route path&amp;gt;` and `&amp;lt;Link to&amp;gt;` 
* `&amp;lt;Route exact&amp;gt;` is gone. Instead, `&amp;lt;Route&amp;gt;` automatically finds the exact matches, and use a  * in their end of the path to indicate they match deeply

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

&lt;/div&gt;



&lt;p&gt;Valid route paths: &lt;br&gt;
/groups&lt;br&gt;
/groups/admin&lt;br&gt;
/users/:id&lt;br&gt;
/users/:id/messages&lt;br&gt;
/files/*&lt;br&gt;
/files/:id/*&lt;/p&gt;

&lt;p&gt;Invalid route paths:&lt;br&gt;
/users/:id?&lt;br&gt;
/tweets/:id(\d+)&lt;br&gt;
/files/&lt;em&gt;/cat.jpg&lt;br&gt;
/files-&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  👉 Read from v6 useParams() instead of v5 props.match
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+ import { useParams } from "react-router-dom-v5-compat";

function Project(props) {
-    const { params } = props.match;
+    const params = useParams();
     // ...
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Use ‘useNavigate()’ instead of ‘useHistory()’:
&lt;/h2&gt;

&lt;p&gt;From (v5) :&lt;br&gt;
&lt;/p&gt;

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

function App() {
  let history = useHistory();
  function gotoHome() {
    history.push("/home");
  }
  function backToLogin() {
    history.replace("/login");
  }
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;button onClick={gotoHome}&amp;gt;go to Home&amp;lt;/button
      &amp;lt;button onClick={backToLogin}&amp;gt;login&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to (v6) :&lt;br&gt;
&lt;/p&gt;

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

function App() {
  let navigate = useNavigate();
 function gotoHome() {
    navigate("/home");
  }
  function backToLogin() {
    navigate("/login", { replace: true });
  }
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;button onClick={gotoHome}&amp;gt;go to Home&amp;lt;/button
      &amp;lt;button onClick={backToLogin}&amp;gt;login&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Use &lt;code&gt;&amp;lt;Navigate&amp;gt;&lt;/code&gt; instead of &lt;code&gt;&amp;lt;Redirect&amp;gt;&lt;/code&gt;:
&lt;/h2&gt;

&lt;p&gt;From (v5) :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Redirect to="about" /&amp;gt;
&amp;lt;Redirect to="home" push /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to (v6) :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Navigate to="about" replace /&amp;gt;
&amp;lt;Navigate to="home" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Replace &lt;code&gt;useRouteMatch&lt;/code&gt; with &lt;code&gt;useMatch&lt;/code&gt;:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useMatch&lt;/code&gt; is very similar to v5's &lt;code&gt;useRouteMatch&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  NavLink ActiveClassName removed:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;NavLink
  to="/messages"
  className="nav-link"
  activeClassName="activated"
  className={({ isActive }) =&amp;gt; "nav-link" + (isActive ? " activated" : "")}
&amp;gt;
  Messages
&amp;lt;/NavLink&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Remove &lt;code&gt;&amp;lt;Prompt&amp;gt;&lt;/code&gt;💔:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Prompt&amp;gt;&lt;/code&gt; from v5 is not currently suppported in v6&lt;/p&gt;

&lt;h2&gt;
  
  
  👉 Remove the compatibility package
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm uninstall react-router-dom-v5-compat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  👉 Uninstall react-router and history
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm uninstall react-router history
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  👉 Install React Router v6
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react-router-dom@6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Phase 3: 🚀
&lt;/h3&gt;

&lt;p&gt;Once we migrated all of our code we can remove all the &lt;code&gt;&amp;lt;CompatRoute&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;CompatRouter&amp;gt;&lt;/code&gt; and compatibility package &lt;code&gt;(react-router-dom-v5-compat)&lt;/code&gt; and install React Router DOM v6 directly.&lt;br&gt;
&lt;/p&gt;

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

  export function App() {
    return (
      &amp;lt;BrowserRouter&amp;gt;
        &amp;lt;Routes&amp;gt;
          &amp;lt;Route path="/" element={&amp;lt;Home /&amp;gt;} /&amp;gt;
          {/* ... */}
        &amp;lt;/Routes&amp;gt;
      &amp;lt;/BrowserRouter&amp;gt;
    );
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we fully migrated to React Router V6 🎉 🎉&lt;/p&gt;

&lt;p&gt;Don't forget to comment down if you're stuck, add your own tips, and help others with their questions 🙏&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactrouter</category>
    </item>
    <item>
      <title>JavaScript Filter</title>
      <dc:creator>k.Jyothi Prakash Reddy</dc:creator>
      <pubDate>Sat, 20 Nov 2021 05:09:33 +0000</pubDate>
      <link>https://forem.com/jyothiprakashk/javascript-filter-4i9a</link>
      <guid>https://forem.com/jyothiprakashk/javascript-filter-4i9a</guid>
      <description>&lt;ul&gt;
&lt;li&gt;Definition&lt;/li&gt;
&lt;li&gt;Filter Tips&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a id="def"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Definition
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;filter()&lt;/code&gt; method returns new array with all the elements that pass the test implemented by provided function.&lt;/p&gt;

&lt;p&gt;&lt;a id="tips"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Filter Tips
&lt;/h1&gt;

&lt;p&gt;Check below code for filter.&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;If&lt;/span&gt; &lt;span class="nx"&gt;you&lt;/span&gt; &lt;span class="nx"&gt;have&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt; &lt;span class="nx"&gt;like&lt;/span&gt; &lt;span class="k"&gt;this&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;data&lt;/span&gt;&lt;span class="o"&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;prakash&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bhanu&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mohan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Then instead of writing code like this &lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;select_user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;prakash&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can simplify code as shown it below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;selected_user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;prakash&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The above code will return true,if the condition is satisfied otherwise it will return false&lt;/p&gt;

&lt;p&gt;We can simplify above code much more simple and understandable using &lt;code&gt;ES6&lt;/code&gt; syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;selected_user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;prakash&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;a id="conclusion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Filter method returns a new array consisting only those &lt;br&gt;
elements that satisfied the provided function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Filter method does not change original array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Filter method does not execute function for empty elements.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I hope You will learn something from this post.If there are more usecases, please mention in below comment section.&lt;/p&gt;

&lt;p&gt;Thanks.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Difference between forEach and map.</title>
      <dc:creator>k.Jyothi Prakash Reddy</dc:creator>
      <pubDate>Thu, 18 Nov 2021 05:03:19 +0000</pubDate>
      <link>https://forem.com/jyothiprakashk/difference-between-foreach-and-map-2674</link>
      <guid>https://forem.com/jyothiprakashk/difference-between-foreach-and-map-2674</guid>
      <description>&lt;p&gt;Javascript has some handy methods which is usefull to iterate arrays.The most two common methods is &lt;strong&gt;&lt;code&gt;Array.prototype.forEach()&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;Array.prototype.map()&lt;/code&gt;&lt;/strong&gt;.we can iterate arrays with both methods but output is different.This methods is unclear for many developers, espescially for beginners.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Definition&lt;/li&gt;
&lt;li&gt;1.Return value&lt;/li&gt;
&lt;li&gt;2.The ability to chain other methods&lt;/li&gt;
&lt;li&gt;4.When to use? what?&lt;/li&gt;
&lt;li&gt;Final Thoughts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Definition
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;map()&lt;/code&gt; method creates a &lt;strong&gt;new array&lt;/strong&gt; every time with the populated results of calling a provided function on every element in the calling array.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;forEach()&lt;/code&gt; method executes with the provided function once for each array element.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Return value
&lt;/h1&gt;

&lt;p&gt;The major difference between &lt;code&gt;map()&lt;/code&gt; and &lt;code&gt;forEach()&lt;/code&gt; is returning value.map returns new array with transformed elements based on the function which you written and even if they do same job, The returning value is &lt;code&gt;undefined&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;jyothiprakash&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bhanu&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;nikitha&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;newArray&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;d&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="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="c1"&gt;//&amp;gt;&amp;gt;&amp;gt;&amp;gt;[{_id: "jyothiprakash"},{_id: "bhanu"},{_id: "nikitha"}]&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newArray&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;d&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="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="c1"&gt;//&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;return value: undefined&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Ability to chain
&lt;/h1&gt;

&lt;p&gt;The difference between &lt;code&gt;map()&lt;/code&gt; and &lt;code&gt;forEach()&lt;/code&gt; is chain with other methods.&lt;code&gt;map&lt;/code&gt; is chainable but &lt;code&gt;forEach&lt;/code&gt; isn't.&lt;/p&gt;

&lt;p&gt;This means map can chain with other methods like &lt;code&gt;reduce()&lt;/code&gt;,&lt;code&gt;sort()&lt;/code&gt; etc.But that is not possible with forEach because it will return undefined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// return value:30&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;//Cannot read properties of undefined (reading 'reduce')"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  when to use &lt;code&gt;map()&lt;/code&gt;? and when to use &lt;code&gt;forEach()&lt;/code&gt;?
&lt;/h1&gt;

&lt;p&gt;Choice between forEach and map is depends on your usecase.&lt;/p&gt;

&lt;p&gt;If you want to change,alternate or use data you can pick &lt;code&gt;map&lt;/code&gt; because it will return a new array.&lt;/p&gt;

&lt;p&gt;If you don't want returning array you can use &lt;code&gt;forEach&lt;/code&gt; or even &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Final Thoughts
&lt;/h1&gt;

&lt;p&gt;1.Just about anything you can with &lt;code&gt;forEach()&lt;/code&gt; and &lt;code&gt;map()&lt;/code&gt; methods.&lt;/p&gt;

&lt;p&gt;2.map allocates memory and always stores &lt;code&gt;return&lt;/code&gt; value.forEach through away return values and always return &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;3.forEach will allow a callback function to mutate current array but map won't mutate current array instead return new array.&lt;/p&gt;

&lt;p&gt;I hope this post will clear difference between map and forEach methods.If there are more methods please mention in comment section.&lt;/p&gt;

&lt;p&gt;If this post is helpful please click on ❤️ Tab.&lt;/p&gt;

&lt;p&gt;Thanks.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript:What is Pure Functions and What is the use?</title>
      <dc:creator>k.Jyothi Prakash Reddy</dc:creator>
      <pubDate>Mon, 19 Apr 2021 02:07:38 +0000</pubDate>
      <link>https://forem.com/jyothiprakashk/javascript-what-is-pure-functions-and-what-is-the-use-3oj7</link>
      <guid>https://forem.com/jyothiprakashk/javascript-what-is-pure-functions-and-what-is-the-use-3oj7</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frtmavncpr89rib3xhavb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frtmavncpr89rib3xhavb.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When I heard about the Pure Functions in JavaScript, I got shocked. What about the regular functions in JavaScript?. Is there any difference b/w Pure and regular functions?.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Pure Function?
&lt;/h3&gt;

&lt;p&gt;1)The function always returns the same result if the same arguments passed in.it doesn't depend on any state, variable, data and change in program execution.it must only depends on program execution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const data = (a, b) =&amp;gt; a + b;
data(2, 4);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TO&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = 6;
const sum = (b) =&amp;gt; {
  return (a += b);
};
sum(4);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Pure Functions:
&lt;/h3&gt;

&lt;p&gt;If you see above first code block.it is not depending on any variable or state. Based on the input data it will give output without affecting.&lt;br&gt;
if You pass &lt;code&gt;2&lt;/code&gt; and &lt;code&gt;4&lt;/code&gt; you will get &lt;code&gt;6&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So there is no side effects in pure functions.&lt;/p&gt;
&lt;h3&gt;
  
  
  ImPure Functions:
&lt;/h3&gt;

&lt;p&gt;The second example returns nothing. It depends on the variable which is outside the scope.&lt;br&gt;
You will get different results when you called function.the first time result is &lt;code&gt;10&lt;/code&gt; and next time result is &lt;code&gt;12&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Side Effects:
&lt;/h3&gt;

&lt;p&gt;1)mutating Input Directly.&lt;br&gt;
2)HTTP Calls.&lt;br&gt;
3)logging.&lt;br&gt;
4)Manipulating DOM.&lt;/p&gt;
&lt;h3&gt;
  
  
  How to Convert Impure to Pure Functions?
&lt;/h3&gt;

&lt;p&gt;####Impure Function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const impurefunc = (name, key, mainobject) =&amp;gt; {
  mainobject[key] = name;
};
const human = {
  name: "Jyothi Prakash",
};
let data = impurefunc("Mohan", 45, human);
console.log(data, human);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variable human always changed because our function introduced as an assignment variable.&lt;/p&gt;

&lt;p&gt;We Can purify the impurefunc as pure function with desire properties.&lt;/p&gt;

&lt;h4&gt;
  
  
  Converting to Pure:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const impurefunc = (key, name, mainobject) =&amp;gt; {
  const newObject = { ...mainobject };
  newObject[key] = name;
  return newObject;
};
const human = {
  name: "Jyothi Prakash",
};
let data = impurefunc("Mohan", 45, human);
console.log(data, human);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Mutating your input can be dangerous, but copy of the original object no problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary:
&lt;/h3&gt;

&lt;p&gt;1)You can clone and then mutate your input. Without touching the original one.&lt;br&gt;
2)Spread Syntax &lt;code&gt;(...object)&lt;/code&gt; is the easiest way for cloning shallowly objects&lt;/p&gt;

&lt;p&gt;Thanks for reading this article.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>ReactJS Dark mode using local storage(Part-2)</title>
      <dc:creator>k.Jyothi Prakash Reddy</dc:creator>
      <pubDate>Fri, 22 May 2020 02:28:17 +0000</pubDate>
      <link>https://forem.com/jyothiprakashk/reactjs-dark-mode-using-local-storage-part-2-17a7</link>
      <guid>https://forem.com/jyothiprakashk/reactjs-dark-mode-using-local-storage-part-2-17a7</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--e06oahJQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/wckrc8w9gd99ewex3axv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e06oahJQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/wckrc8w9gd99ewex3axv.png" alt="Alt Text" width="314" height="226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In previous blog post we discussed local storage, and how local storage is working and how data is stored in local storage and how retrieve data from ls.&lt;/p&gt;

&lt;p&gt;In order to follow rest of the article, I would recommend to to read part-1 blog post.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f0XOBmmB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/ehh8ma9u16173wrhvy8h.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f0XOBmmB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/ehh8ma9u16173wrhvy8h.jpeg" alt="Alt Text" width="271" height="186"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://dev.to/jyothiprakash097/reactjs-dark-mode-using-local-storage-introduction-part1-12j0"&gt;ReactJS Dark mode using local storage(Introduction Part1)&lt;/a&gt;
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Discussions: useState(),useEffect()
&lt;/h3&gt;

&lt;h3&gt;
  
  
  useState()
&lt;/h3&gt;

&lt;p&gt;useState is a hook that let you add react state to functional components.since React hooks has been released functional components can use state.&lt;/p&gt;

&lt;h3&gt;
  
  
  syntax
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [dark, setDark] = useState("");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  useEffect()
&lt;/h3&gt;

&lt;p&gt;If you are familiar with class based lifecycles method then you will think about useEffect Hook as componentDidMount, componentDidUpdate, and componentWillUnmount combined.&lt;/p&gt;

&lt;p&gt;Life cycles method is very important in class based components and sometimes you want to fetch data from API and when render a component.&lt;br&gt;
sometimes you want to do specific action when your component updates.In lifecycle methods most important methods are componentDidMount and  componentDidUpdate.useEffect allow us to handle our logic in lifecycles methods.useEffect called every time when your component is changed.&lt;/p&gt;
&lt;h3&gt;
  
  
  Lets dive into code
&lt;/h3&gt;
&lt;h3&gt;
  
  
  index.js
&lt;/h3&gt;


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



function App() {
  const [dark, setDark] = useState(getMode);

  useEffect(() =&amp;gt; {
    localStorage.setItem("dark", JSON.stringify(dark));
  }, [dark]);

  function getMode() {
    const savedmode = JSON.parse(localStorage.getItem("dark"));
    return savedmode || false;
  }
  return(
    &amp;lt;div&amp;gt;
    &amp;lt;h1 style={{textAlign:"center"}}&amp;gt;Darkmode with ReactJS&amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}
ReactDOM.render(&amp;lt;App /&amp;gt;,document.getElementById('root'));

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

&lt;/div&gt;

&lt;h3&gt;
  
  
  Output
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hMd0amnZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/3oab0qp4l83pjcex6ugt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hMd0amnZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/3oab0qp4l83pjcex6ugt.png" alt="Alt Text" width="800" height="371"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is how local storage is working in live example and &lt;strong&gt;setItem()&lt;/strong&gt; is used to set the value in localstorage and &lt;strong&gt;getItem()&lt;/strong&gt; is used to retrive the data from localstorage.&lt;/p&gt;
&lt;h3&gt;
  
  
  App.css
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.black {
  background-color: black;
  color: white;
}
.light {
  background-color: blanchedalmond;
  color: black;
}
.nav {
  background-color: blue;
  color: white;
}
.toggle-container {
  background-color: blue;
  padding: 20px;
  display: flex;
  justify-content: center;
}

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

&lt;/div&gt;


&lt;p&gt;This basic CSS what I did for dark mode.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return (
    &amp;lt;div className={dark ? "black" : "light"} style={{ height: "100vh" }}&amp;gt;
      &amp;lt;nav&amp;gt;
        &amp;lt;div className="toggle-container"&amp;gt;
          &amp;lt;span style={{ color: dark ? "grey" : "yellow" }}&amp;gt;☀︎&amp;lt;/span&amp;gt;
          &amp;lt;span className="toggle"&amp;gt;
            &amp;lt;input
              checked={dark}
              onChange={() =&amp;gt; setDark((prevMode) =&amp;gt; !prevMode)}
              id="checkbox"
              className="checkbox"
              type="checkbox"
            /&amp;gt;
            &amp;lt;label htmlFor="checkbox" /&amp;gt;
          &amp;lt;/span&amp;gt;
          &amp;lt;span style={{ color: dark ? "slateblue" : "grey" }}&amp;gt;☾&amp;lt;/span&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/nav&amp;gt;

      &amp;lt;div style={{ textAlign: "center" }}&amp;gt;
        &amp;lt;h1&amp;gt;{dark ? "darkmode" : "lightmode"}&amp;lt;/h1&amp;gt;
        &amp;lt;p&amp;gt;This is dark mode content&amp;lt;/p&amp;gt;
        &amp;lt;h1&amp;gt;The darkmode is implemented by reactjs&amp;lt;/h1&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The logic behind dark mode in ReactJS and Iam checking previous mode is not equal to current mode then the display the mode based on logic.&lt;/p&gt;

&lt;h3&gt;
  
  
  Final code
&lt;/h3&gt;



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



function App() {
  const [dark, setDark] = useState(getMode);

  useEffect(() =&amp;gt; {
    localStorage.setItem("dark", JSON.stringify(dark));
  }, [dark]);

  function getMode() {
    const savedmode = JSON.parse(localStorage.getItem("dark"));
    return savedmode || false;
  }
  return (
    &amp;lt;div className={dark ? "black" : "light"} style={{ height: "100vh" }}&amp;gt;
      &amp;lt;nav&amp;gt;
        &amp;lt;div className="toggle-container"&amp;gt;
          &amp;lt;span style={{ color: dark ? "grey" : "yellow" }}&amp;gt;☀︎&amp;lt;/span&amp;gt;
          &amp;lt;span className="toggle"&amp;gt;
            &amp;lt;input
              checked={dark}
              onChange={() =&amp;gt; setDark((prevMode) =&amp;gt; !prevMode)}
              id="checkbox"
              className="checkbox"
              type="checkbox"
            /&amp;gt;
            &amp;lt;label htmlFor="checkbox" /&amp;gt;
          &amp;lt;/span&amp;gt;
          &amp;lt;span style={{ color: dark ? "slateblue" : "grey" }}&amp;gt;☾&amp;lt;/span&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/nav&amp;gt;

      &amp;lt;div style={{ textAlign: "center" }}&amp;gt;
        &amp;lt;h1&amp;gt;{dark ? "darkmode" : "lightmode"}&amp;lt;/h1&amp;gt;
        &amp;lt;p&amp;gt;This is dark mode content&amp;lt;/p&amp;gt;
        &amp;lt;h1&amp;gt;The darkmode is implemented by reactjs&amp;lt;/h1&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
ReactDOM.render(&amp;lt;App /&amp;gt;,document.getElementById('root'));

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Output
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TWFgg0iW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/r3irebtflxph76bi4rc1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TWFgg0iW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/r3irebtflxph76bi4rc1.png" alt="Alt Text" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  conclusion
&lt;/h3&gt;

&lt;p&gt;I did this blog post for whom don't know about reactjs and localstorage.I thought you will learn little bit from this article.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thank you guys!!!..&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>ReactJS Dark mode using local storage(Introduction Part1)</title>
      <dc:creator>k.Jyothi Prakash Reddy</dc:creator>
      <pubDate>Thu, 21 May 2020 02:35:18 +0000</pubDate>
      <link>https://forem.com/jyothiprakashk/reactjs-dark-mode-using-local-storage-introduction-part1-12j0</link>
      <guid>https://forem.com/jyothiprakashk/reactjs-dark-mode-using-local-storage-introduction-part1-12j0</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JCWl27Aj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/snh6farlz5t5fw3uu2bv.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JCWl27Aj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/snh6farlz5t5fw3uu2bv.jpeg" alt="Alt Text" width="271" height="186"&gt;&lt;/a&gt;&lt;br&gt;
Discussions:React &lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;Hooks&lt;/a&gt;, &lt;a href="https://www.w3schools.com/jsreF/prop_win_localstorage.asp"&gt;local-Storage&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Introduction:
&lt;/h3&gt;

&lt;p&gt;Today I am going to discuss dark-mode in ReactJS, recently I started learning ReactJS, I struggled much time for how dark mode is working in local storage. After struggling, I made a post in ReactJS dark mode.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is local-storage ?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.w3schools.com/jsreF/prop_win_localstorage.asp"&gt;local-Storage&lt;/a&gt; is a web storage that allows &lt;a href="https://www.w3schools.com/js/"&gt;JS&lt;/a&gt; sites and apps to store and access data right in the browser with no expiration date.the data is stored in browser even after the browser window is closed.&lt;/p&gt;
&lt;h3&gt;
  
  
  Hooks
&lt;/h3&gt;

&lt;p&gt;Hooks are functions, they let you use &lt;a href="https://reactjs.org/"&gt;React&lt;/a&gt; without classes, we mean that hooks allow us to easily manipulate the state of our functional component without needing to convert them into class components.&lt;/p&gt;
&lt;h3&gt;
  
  
  There are three methods to choose from:
&lt;/h3&gt;

&lt;p&gt;1) &lt;code&gt;setItem()&lt;/code&gt;: Add key and value to localStorage&lt;br&gt;
2) &lt;code&gt;getItem()&lt;/code&gt;: Retrieve a value by the key from localStorage&lt;br&gt;
3) &lt;code&gt;removeItem()&lt;/code&gt;: Remove an item by key from localStorage&lt;/p&gt;
&lt;h3&gt;
  
  
  setItem()
&lt;/h3&gt;

&lt;p&gt;It takes two parameters: a key and a value. The key can be referenced later to fetch the value attached to it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
localStorage.setItem('name', 'python');&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Where name is key and Jyothi Prakash is value.note that local storage only store strings&lt;br&gt;
To store arrays or objects you should convert them to strings.To do this we should use JSON.stringify() method before passing to SetItem().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const data={
name:"python",
framework:"Django"
}
localstorage.setItem("language",JSON.stringify(data))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;span&gt;getItem()&lt;/span&gt;
&lt;/h3&gt;

&lt;p&gt;getItem() method is used to access the data stored in local storage object.&lt;/p&gt;

&lt;p&gt;It accepts only &lt;strong&gt;key&lt;/strong&gt; and returns a &lt;strong&gt;value&lt;/strong&gt; as string&lt;/p&gt;

&lt;p&gt;&lt;code&gt;localstorage.getItem("language")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This return a string with the value:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;“{“name”:” python”,” framework”:” Django”}”&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To this value you have converted it back to object.&lt;br&gt;
To do this we use JSON.parse() method which is convert JSON string to object.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const value=JSON.parse(localstorage.getItem("language"))&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  removeItem()
&lt;/h3&gt;

&lt;p&gt;removeitem() is used to remove the key in local storage if it exists.if there is no key, the method will do nothing.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;localstorage.removeItem("language")&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  localstorage Limitations
&lt;/h3&gt;

&lt;p&gt;1) local storage is limited to 5 MB across all browsers.&lt;br&gt;
2) Don't store sensitive information in local storage.&lt;br&gt;
3) local storage can only be read by the client-side&lt;/p&gt;

&lt;p&gt;I hope everyone likes this post, This is my first Blog post in DEV and Here is &lt;a href="https://dev.to/jyothiprakash/reactjs-dark-mode-using-local-storage-part-2-17a7"&gt;Part-2&lt;/a&gt;&lt;/p&gt;

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