{"id":739,"date":"2024-02-05T11:13:21","date_gmt":"2024-02-05T11:13:21","guid":{"rendered":"https:\/\/www.cmsgalaxy.com\/blog\/?p=739"},"modified":"2024-02-05T11:13:23","modified_gmt":"2024-02-05T11:13:23","slug":"use-of-localstorage-in-react-application","status":"publish","type":"post","link":"https:\/\/www.cmsgalaxy.com\/blog\/use-of-localstorage-in-react-application\/","title":{"rendered":"Use of LocalStorage in React Application"},"content":{"rendered":"\n<p>In React.js, you can use the browser&#8217;s <code>localStorage<\/code> API to store data locally within the user&#8217;s browser. This allows you to persist data across sessions and page reloads, making it useful for storing user preferences, authentication tokens, or any other data that needs to be preserved between visits to your application.<\/p>\n\n\n\n<p>Here&#8217;s how you can use <code>localStorage<\/code> in a React application:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Setting Data:<\/strong><br>You can store data in <code>localStorage<\/code> using the <code>setItem()<\/code> method. This method takes two arguments: a key and a value. The value must be a string.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   localStorage.setItem('key', 'value');<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Getting Data:<\/strong><br>You can retrieve data from <code>localStorage<\/code> using the <code>getItem()<\/code> method. This method takes the key of the item you want to retrieve and returns its value as a string.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   const value = localStorage.getItem('key');<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>Removing Data:<\/strong><br>You can remove data from <code>localStorage<\/code> using the <code>removeItem()<\/code> method. This method takes the key of the item you want to remove.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   localStorage.removeItem('key');<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li><strong>Example in React Component:<\/strong><br>Here&#8217;s an example of how you can use <code>localStorage<\/code> in a React component to persist and retrieve data:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   import React, { useState, useEffect } from 'react';\n\n   function MyComponent() {\n       const &#91;value, setValue] = useState('');\n\n       useEffect(() =&gt; {\n           \/\/ Retrieve data from localStorage when component mounts\n           const storedValue = localStorage.getItem('myValue');\n           if (storedValue) {\n               setValue(storedValue);\n           }\n       }, &#91;]);\n\n       const handleChange = (event) =&gt; {\n           const newValue = event.target.value;\n           setValue(newValue);\n           \/\/ Store data in localStorage when value changes\n           localStorage.setItem('myValue', newValue);\n       };\n\n       return (\n           &lt;div&gt;\n               &lt;input type=\"text\" value={value} onChange={handleChange} \/&gt;\n               &lt;p&gt;Stored Value: {value}&lt;\/p&gt;\n           &lt;\/div&gt;\n       );\n   }<\/code><\/pre>\n\n\n\n<p>In this example, the component initializes its state with the value stored in <code>localStorage<\/code> when it mounts. When the user changes the input value, the component updates its state and stores the new value in <code>localStorage<\/code>. This ensures that the input value is persisted between page reloads.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In React.js, you can use the browser&#8217;s localStorage API to store data locally within the user&#8217;s browser. This allows you<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-739","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/comments?post=739"}],"version-history":[{"count":1,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/739\/revisions"}],"predecessor-version":[{"id":740,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/739\/revisions\/740"}],"wp:attachment":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/media?parent=739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/categories?post=739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/tags?post=739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}