Skip to content

Instantly share code, notes, and snippets.

@muraray
Last active September 14, 2018 04:56
Show Gist options
  • Save muraray/1992614286ada906edd4842fd01a1c8e to your computer and use it in GitHub Desktop.
Save muraray/1992614286ada906edd4842fd01a1c8e to your computer and use it in GitHub Desktop.
How to Localstorage
const unique_incr = () =>
('' + 1e7 + -1e3 + -4e3 + -8e3 + -1e11).replace(/1|0/g, function() {
return (0 | (Math.random() * 16)).toString(16)
})
const STORAGE_KEY = 'todos';
var fetch = () => JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]')
var save = todos => localStorage.setItem(STORAGE_KEY, JSON.stringify(todos))
var state = { todos: [] }
const todos = state.todos.concat({
id: unique_incr(),
done: false,
editing: false,
value: "Things to search for"
});
todos
save(todos)
fetch()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment