Home > database >  How to change the data stored in LocalStorage when clicking on element in react?
How to change the data stored in LocalStorage when clicking on element in react?

Time:02-25

I am creating to-do app in react, and storing the data in localstorage,when user click on particular task it is mark completed, for that purpose i have "complete" boolean property for all task in localStorage.now i want to change that property onclick of that particular task,How to achieve this?.Here is the code link : https://github.com/Khatri-Jinal/react-app/tree/practical4

CodePudding user response:

get the value from local storage and update it and then set it again for ex: let data=localStorage.getItem('tasks') //make changes in data localStorage.setItem("tasks", JSON.stringify(tasks));

CodePudding user response:

I suggest you make a custom hook for local storage (ie. useLocalStorage). This ensures that when you update a value in the local storage, the components that are using the updated value are automatically re-rendered.

You may look it up online or use this youtube viode for your reference. The first example there is useLocalStorage.

  • Related