<?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: TinTin</title>
    <description>The latest articles on DEV Community by TinTin (@tintin-mn77).</description>
    <link>https://dev.to/tintin-mn77</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%2F2770274%2F825efc7c-1e28-47a6-8388-c383c8e0d8db.jpg</url>
      <title>DEV Community: TinTin</title>
      <link>https://dev.to/tintin-mn77</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tintin-mn77"/>
    <language>en</language>
    <item>
      <title>Everything About Local Storage in JavaScript and React</title>
      <dc:creator>TinTin</dc:creator>
      <pubDate>Wed, 19 Feb 2025 17:16:39 +0000</pubDate>
      <link>https://dev.to/tintin-mn77/everything-about-local-storage-in-javascript-and-react-1h58</link>
      <guid>https://dev.to/tintin-mn77/everything-about-local-storage-in-javascript-and-react-1h58</guid>
      <description>&lt;p&gt;Of course, you've read a lot about localStorage and still feel confused? Let's dive deep!&lt;/p&gt;

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

&lt;p&gt;LocalStorage is one of the simplest ways to store data in the browser persistently. It saves data as key-value pairs (always string format) and remains even after the user closes, refreshes or reopens the browser.&lt;br&gt;
LocalStorage is a built-in property of the window object, making it globally accessible in the browser.&lt;/p&gt;


&lt;h2&gt;
  
  
  localStorage vs. sessionStorage vs. cookies
&lt;/h2&gt;

&lt;p&gt;Before diving into localStorage, let’s  compare to other storage methods in the browser.&lt;br&gt;
&lt;strong&gt;SessionStorage&lt;/strong&gt;: similar to localStorage but only persists until the tab is closed.&lt;br&gt;
&lt;strong&gt;Cookies&lt;/strong&gt;: store small amounts of data and can be sent with every HTTP request.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4czfah93inurf1h5fmf6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4czfah93inurf1h5fmf6.jpg" alt="localstorage" width="800" height="282"&gt;&lt;/a&gt;&lt;br&gt;
Now you understand that LocalStorage is often preferred when dealing with persistent client-side data that does not need to be sent to the server with every request.&lt;/p&gt;


&lt;h2&gt;
  
  
  How to Use localStorage in JavaScript
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage" rel="noopener noreferrer"&gt;localStorage &lt;/a&gt;provides a simple API for storing, retrieving, and deleting data.&lt;/p&gt;
&lt;h3&gt;
  
  
  Storing/Set Data
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;localStorage.setItem("username", "titntin");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Retrieving/Get Data
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const user = localStorage.getItem("username");
console.log(user); // Output: tintin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Removing One Data
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;localStorage.removeItem("username");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Clearing All Data
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;localStorage.clear();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using localStorage in React
&lt;/h2&gt;

&lt;p&gt;When using localStorage in React, we can integrate it with the component state using &lt;code&gt;useEffect&lt;/code&gt;and &lt;code&gt;useState&lt;/code&gt;. (However, a better approach is to create a custom hook.)&lt;/p&gt;
&lt;h3&gt;
  
  
  Example: Persisting Theme Preference in React Without Custom Hook
&lt;/h3&gt;

&lt;p&gt;(You can copy the code in your editor and see the result. but first figure it out)&lt;br&gt;
&lt;/p&gt;

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

function ThemeSwitcher() {
    const [theme, setTheme] = useState(localStorage.getItem("theme") || "light");

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

    const toggleTheme = () =&amp;gt; {
        setTheme(theme === "light" ? "dark" : "light");
    };

    return (
        &amp;lt;div style={{ background: theme === "light" ? "#fff" : "#333", color: theme === "light" ? "#000" : "#fff", padding: "20px" }}&amp;gt;
            &amp;lt;p&amp;gt;Current Theme: {theme}&amp;lt;/p&amp;gt;
            &amp;lt;button onClick={toggleTheme}&amp;gt;Toggle Theme&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
    );
}

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

&lt;/div&gt;



&lt;p&gt;This code saves the theme in &lt;code&gt;localStorage&lt;/code&gt;, ensuring that the user's theme preference remains even after refreshing the page.&lt;/p&gt;

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




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

&lt;p&gt;localStorage allows us to store data &lt;strong&gt;independently from the server&lt;/strong&gt;, meaning we can keep certain user preferences and information directly in the browser. This helps improve performance and user experience.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is this useful?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Faster user experience&lt;/strong&gt; → No need to send unnecessary requests to the server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;User settings persistence&lt;/strong&gt; → Like saving dark/light mode, selected language, or shopping cart items.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reduces server load&lt;/strong&gt; → Since frequently used data is already stored locally.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, imagine an online store where you add products to your cart. With localStorage, even if you close and reopen the browser, your cart items will still be there!&lt;/p&gt;




&lt;h2&gt;
  
  
  Advantages of localStorage
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Persistence&lt;/strong&gt;: Data remains even after refreshing or reopening the browser. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simple API&lt;/strong&gt;: Easy to use with &lt;code&gt;setItem&lt;/code&gt;, &lt;code&gt;getItem&lt;/code&gt;, and &lt;code&gt;removeItem&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Good for Lightweight Data&lt;/strong&gt;: Ideal for storing small JSON objects or preferences. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No Network Overhead&lt;/strong&gt;: Unlike cookies, data isn’t sent with every HTTP request.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Limitations and Security Concerns
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Limited Storage&lt;/strong&gt;: Only ~5MB of data can be stored. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No Expiration&lt;/strong&gt;: Data remains unless explicitly removed. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security Risks&lt;/strong&gt;: Susceptible to XSS attacks if data isn’t handled properly. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Not for Sensitive Data&lt;/strong&gt;: Never store passwords or personal user data in Local Storage.&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;localStorage is a powerful tool for maintaining client-side data, especially in JavaScript and React applications. However, it should be used cautiously, considering its security limitations and storage constraints. For more advanced state management and persistence, libraries like &lt;a href="https://redux-toolkit.js.org/rtk-query/usage/persistence-and-rehydration" rel="noopener noreferrer"&gt;Redux Persist&lt;/a&gt; or &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API" rel="noopener noreferrer"&gt;IndexedDB&lt;/a&gt; might be better suited.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;hey guys, this is my first article on &lt;a href="https://dev.to/"&gt;DEV&lt;/a&gt;, so I’d love to hear your thoughts and feedback. Let me know if you have any suggestions or questions—I’m here to learn and improve! :))&lt;/p&gt;
&lt;/blockquote&gt;

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