'Hello, so im using localstorage to store my input data from tags created. so i have multiple input fields,
So basically the issue i'm having is:
For every user, lets say theres tom and daniel, with different details each, with an input bar to add tags.
So if i add two tags to tom it shows the 2 tag data in the local storage, but when i refresh the page it puts the data back in tom to keep it but now it adds the same data for everyone else.
Secondly when i now try to add tags to Daniel after i added tags to tom it overwrites the local storage to show only the tags for Daniel.
And when i refresh the page it does the same thing as it did with tom where now updates everyone else in the list to have the same tags.
If anyone has any idea how i could solve this please let me know.
Right now im thinking if i can make it create a new object everytime a new input is made in a different field but not sure how to do this.
And by the way before the refresh, the tag data shows in the right place in the screen itself.
So everything is working and showing correctly on the page just not in local storage.
Thats why when i refresh it updates everything else to have the same tag of the last tag data instead of having different tags for each.
the code for useLocalStorage.js file
import { useState } from "react";
// Hook
function getSavedValue(key, initialValue) {
const savedValue=JSON.parse(localStorage.getItem(key, initialValue))
if(savedValue) return savedValue;
if(initialValue instanceof Function) return initialValue()
return initialValue;
}
export default function useLocalStorage(key, intialValue) {
const [value, setValue] = useState(() => {
return getSavedValue(key, intialValue);
})
return [value, setValue]
}
Solution 1:[1]
I think (I don't have the entire code so I do my best) you want to store tags for each user, am I right?
So using localStorage is a bad idea, because data is stored in your browser and your script get it for all users ; or replace data when you change it.
Where your users data is stored basically, in a MySQL database or something like?
If yes, it will be judicious to store user tags in your database instead of localStorage, and get it by fetching DB.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Harkhenon |
