How to save new state to local JSON using ReactJS ? Last Updated : 10 Nov, 2023 Comments Improve Suggest changes Like Article Like Report 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 localStorageApproach:In React to save a new state to local JSON we will use the localStorage web API. localStorage API helps to access the storage of the browser to store and manage data. This data may be used for user-specific settings and configuration when the web page is visited. Steps to create React Application:Step 1: Create React App by using the following command. npx create-react-app foldernameStep 2: After creating your project folder, i.e., folder name, move to it using the following command: cd foldernameProject Structure: Example: This example uses localStorage API to set the json data in local storage. JavaScript // Filename - App.js import React, { Component } from "react"; class App extends Component { state = { data: "This is data", num: 123, boolean: true, }; // save data to localStorage saveStateToLocalStorage = () => { localStorage.setItem( "state", JSON.stringify(this.state) ); }; // Fetch data from local storage getStateFromLocalStorage = () => { let data = localStorage.getItem("state"); if (data !== undefined) { this.setState(JSON.parse(data)); } }; componentDidMount() { // Fetch data from local storage this.getStateFromLocalStorage(); } render() { return ( <div> <h2>GeeksforGeeks</h2> <button onClick={this.saveStateToLocalStorage} > Save State to local storage </button> </div> ); } } export default App; Step to Run Application: Run the application using the following command from the root directory of the project: npm startOutput: Now open your browser and go to http://localhost:3000/, you will see the following output. Comment More infoAdvertise with us Next Article How to save new state to local JSON using ReactJS ? P pratikraut0000 Follow Improve Article Tags : Web Technologies ReactJS Web technologies JSON React-Questions +1 More Similar Reads How to Use Flux to Manage State in ReactJS? State management in ReactJS is important for building dynamic and responsive applications. Flux, an architecture for managing state, provides a structured approach to handle data flow and state changes efficiently in React applications. In this article, we will explore the Flux to Manage State in Re 5 min read How to lift state two levels up in ReactJS ? We want to lift the state two levels up such that when an event occurs at the lower level component then it should affect the state two levels up. For example, there are three components X, Y, and Z. X is the parent of Component Y, and Component Y is the parent of Component Z. Prerequisites:NodeJS o 2 min read How to persist state with Local or Session Storage in React ? 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 explic 2 min read How to locally manage component's state in ReactJS ? Any component in React JS majorly depends on its props and state to manage data. A component's state is private to it and is responsible for governing its behavior throughout its life. A state is nothing but a structure that records any data changes in a react application. It can be used for storing 2 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 Like