Skip to content
Permalink
Browse files

Added the ability to persist app state

This adds the ability to persist the application state by storing the
top level state as a string in `localStroage`.
  • Loading branch information...
reddit-coding-exp committed Feb 17, 2019
1 parent 1a7e58f commit 790ce645e24315390825b651b3bca2a9790d3928
Showing with 20 additions and 6 deletions.
  1. +20 −6 src/App.tsx
@@ -12,6 +12,8 @@ function generateId() {
return String(Math.floor(Math.random() * 10000000));
}

const storageKey = "todoAppKey";

type TodoFilter = "all" | "active" | "completed";

interface TodoItemData {
@@ -34,12 +36,24 @@ interface AppProps {}
class App extends Component<AppProps, TodoList> {
constructor(props: AppProps) {
super(props);
this.state = {
currentText: "",
todoItems: [],
activeFilter: "all",
completedState: {}
};

const persistedState = localStorage.getItem(storageKey);

if (persistedState === null) {
this.state = {
currentText: "",
todoItems: [],
activeFilter: "all",
completedState: {}
};
} else {
this.state = JSON.parse(persistedState)
}
}

shouldComponentUpdate(nextProps: AppProps, nextState: TodoList) {
localStorage.setItem(storageKey, JSON.stringify(this.state));
return true;
}

onTodoItemStateChange = (event: TodoItemChangeEvent) => {

0 comments on commit 790ce64

Please sign in to comment.
You can’t perform that action at this time.