I'm working with Marvel's API and in the project I'm working on, all the characters are displayed in the main page. I can click in a character and open a detail page, where I can see the character's information. I also available three text inputs with the name, image link and description, next to an update button. When I enter this page, I save to the local storage the character information. I want to be able to update the local storage information when I click the update button. To this point, I'm able to get all the information I need to update, but I just don't know how to update the local storage array. The object I'm saving in the local storage is something like this: {id: 'characterid', name: 'charactername', description: 'characterdescription', image: 'imagelink'}
const [name, setName] = useState("");
const [description, setDescription] = useState("");
const [image, setImage] = useState("");
useEffect(() => {
const hero = JSON.parse(localStorage.getItem("character"));
setCharacter(hero);
setName(hero.name);
setDescription(hero.description);
setImage(hero.thumbnail.path + "." + hero.thumbnail.extension);
}, []);
function updateCharacter() {
console.warn("item", name, image, description);
}
<input type="text" className="character_name" defaultValue={character.name} onChange={(e) => {setName(e.target.value);}}/>
<input type="text" className="character_image" defaultValue={`${character.thumbnail.path}.${character.thumbnail.extension}`} onChange={(e) => {setImage(e.target.value);}}/>
<input type="text" className="character_description" defaultValue={character.description} onChange={(e) => {setDescription(e.target.value);}}/>
<button onClick={updateCharacter}>Update Character</button>
Thanks for the help!