How to persist state with Local or Session Storage in React ? Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Persisting state with localStorage or sessionStorage is a way to store data in the user's web browser so that it remains available even after the page is refreshed or closed. Persisting state with localStorage or sessionStorage: localStorage vs. sessionStorage:localStorage persists data until explicitly cleared by the user or the web application.sessionStorage persists data only for the duration of the page session. The data is cleared once the session ends (when the browser window is closed).Storing Data:To store data, you can use the setItem() method provided by both localStorage and sessionStorage.It takes two parameters: the key (a string) and the value (a string). localStorage.setItem('username', 'GeeksforGeeks');Retrieving Data: To retrieve data, you use the getItem() method, passing the key as a parameter.const username = localStorage.getItem('username');Updating Data: To update data, you use the setItem() method, similar to the storing data, you jus set a new value for the same key.localStorage.setItem('username', 'GfG');Removing Data:To remove a specific item, you use the removeItem() method and pass the key as a parameter.localStorage.removeItem('username');Clearing All Data:To clear all stored data (all key-value pairs), you can use the clear() method.localStorage.clear();Usage Considerations:Remember that localStorage and sessionStorage can only store strings. If you need to store objects or arrays, you will need to serialize and deserialize them using methods like JSON.stringify() and JSON.parse().To persist state using React's useState hook along with localStorage, you can follow these steps: Initialize State from localStorage: When your component mounts, you can retrieve the initial state from localStorage.Update State and localStorage: Whenever the state changes, update both the state and localStorage to reflect the changes.Example: Below is an example of persisting state with localStorage or sessionStorage. JavaScript import React, { useState, useEffect } from 'react'; function Counter() { const [count, setCount] = useState(() => { const storedCount = localStorage.getItem('count'); return storedCount ? parseInt(storedCount) : 0; }); useEffect(() => { localStorage.setItem('count', count.toString()); }, [count]); const increment = () => { setCount(prevCount => prevCount + 1); }; const decrement = () => { setCount(prevCount => prevCount - 1); }; return ( <div> <h1>Counter: {count}</h1> <button onClick={increment}>Increment</button> <button onClick={decrement}>Decrement</button> </div> ); } export default Counter; Output: Comment More infoAdvertise with us Next Article How to persist state with Local or Session Storage in React ? F faheemakt6ei Follow Improve Article Tags : Web Technologies ReactJS MERN-QnA WebTech-FAQs Similar Reads How to persist Redux state in local Storage without any external library? Redux as per the official documentation is a predictable state container for JavaScript apps. In simple words, it is a state management library, with the help of Redux managing the state of components becomes very easy. We can manage the state of the app by creating a global state known as a store. 8 min read Managing Local Storage & Session Storage using React Hooks To manage Loacal Storage and Session Storage, we can use hooks like useEffect and useState provided by React. Managing local or session storage is a repetitive task, so it is a good practice to create a custom hook that manages the storage properly. In this article, I'll cover the whole process of m 3 min read How to save new state to local JSON using ReactJS ? To save data in local storage is a common task while creating a react application to store the user-specific data. We can save new state to local JSON using react in local storage of the browser and access that information anytime later. Prerequisites:React JSNPM & Node.jsJavaScript localStorage 2 min read Persisting State in React App with Redux Persist Redux Persist is a library used to save the Redux store's state to persistent storage, such as local storage, and rehydrate it when the app loads. In this article, we make a simple counter application using React and Redux, demonstrating state persistence using Redux Persist. This project includes i 3 min read State Management with useState Hook in React useState is a built-in hook that empowers functional components to manage state directly, eliminating the need for class-based components or external state management libraries for simple use cases. It provides an easy mechanism to track dynamic data within a component, enabling it to React to user 3 min read Like