This file contains hidden or 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
| import React, { Component } from "react"; | |
| import logo from "./logo.svg"; | |
| import "./App.css"; | |
| import SimpleStorage from "react-simple-storage"; | |
| class App extends Component { | |
| constructor(props) { | |
| super(props); | |
| this.state = { | |
| newItem: "", |
This file contains hidden or 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
| //... everything is the same above | |
| render() { | |
| return ( | |
| <div className="App"> | |
| {/* This is all you need to add */} | |
| <SimpleStorage parent={this} /> | |
| //...etc. |
This file contains hidden or 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
| import React, { Component } from "react"; | |
| import logo from "./logo.svg"; | |
| import "./App.css"; | |
| import SimpleStorage from "react-simple-storage"; | |
| class App extends Component { | |
| //... etc. |
This file contains hidden or 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
| import React, { Component } from "react"; | |
| import logo from "./logo.svg"; | |
| import "./App.css"; | |
| class App extends Component { | |
| constructor(props) { | |
| super(props); | |
| this.state = { | |
| newItem: "", | |
| list: [] |
This file contains hidden or 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
| componentDidMount() { | |
| this.hydrateStateWithLocalStorage(); | |
| // add event listener to save state to localStorage | |
| // when user leaves/refreshes the page | |
| window.addEventListener( | |
| "beforeunload", | |
| this.saveStateToLocalStorage.bind(this) | |
| ); | |
| } |
This file contains hidden or 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
| saveStateToLocalStorage() { | |
| // for every item in React state | |
| for (let key in this.state) { | |
| // save to localStorage | |
| localStorage.setItem(key, JSON.stringify(this.state[key])); | |
| } | |
| } |
This file contains hidden or 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
| componentDidMount() { | |
| this.hydrateStateWithLocalStorage(); | |
| } |
This file contains hidden or 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
| hydrateStateWithLocalStorage() { | |
| // for all items in state | |
| for (let key in this.state) { | |
| // if the key exists in localStorage | |
| if (localStorage.hasOwnProperty(key)) { | |
| // get the key's value from localStorage | |
| let value = localStorage.getItem(key); | |
| // parse the localStorage string and setState | |
| try { |
This file contains hidden or 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
| deleteItem(id) { | |
| // copy current list of items | |
| const list = [...this.state.list]; | |
| // filter out the item being deleted | |
| const updatedList = list.filter(item => item.id !== id); | |
| this.setState({ list: updatedList }); | |
| // update localStorage | |
| localStorage.setItem("list", JSON.stringify(updatedList)); |
This file contains hidden or 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]; |