Last active
June 3, 2022 01:59
-
-
Save ryanjyost/918bbf284781885f2a97d85c4a9b3c6a to your computer and use it in GitHub Desktop.
addItem() with localStorage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addItem() { | |
// create a new item | |
const newItem = { | |
id: 1 + Math.random(), | |
value: this.state.newItem.slice() | |
}; | |
// copy current list of items | |
const list = [...this.state.list]; | |
// add the new item to the list | |
list.push(newItem); | |
// update state with new list, reset the new item input | |
this.setState({ | |
list, | |
newItem: "" | |
}); | |
// update localStorage | |
localStorage.setItem("list", JSON.stringify(list)); | |
localStorage.setItem("newItem", ""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment