I'm trying to create a simple quest tracker in React, I want the checked value of true to persist in localStorage, but I'm getting this error. I understand that the arguments can only be strings so I tried JSON.stringify the boolean value but it still returns 1 argument. Here is the checkbox I've mapped to each quest in an array.
{props.mainQuests.map((quest, i) => {
return (
<div className="questlist">
<li key={quest} className="quest">
<p className="questName">{quest}</p>
<p className="questDesc">{mainQuestDesc[i]}</p>
<label for="completed">Completed</label>
<input
type="checkbox"
name="completed"
id={quest}
onChange={props.toggleCheckboxChange}
autoComplete="off"
></input>
</li>
</div>
);
And here is the function declared in the App.js component.
toggleCheckboxChange = (e) => {
e.preventDefault();
if (e.target.type === "checkbox") {
const current = JSON.stringify(e.target.checked);
console.log(e.target.id);
console.log(current);
localStorage.setItem({ [e.target.id]: current });
}
};
Both the console logs return string values, so I'm quite confused, any help would be greatly appreciated :)
question from:
https://stackoverflow.com/questions/65936244/localstorage-setitem-at-least-2-arguments-required-but-only-1-passed 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…