Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. import { createState, createEffect } from 'solid-js';
  2. const LOCAL_STORAGE_KEY = 'todos-solid';
  3.  
  4. function createLocalState(value) {
  5. // load stored todos on init
  6. const stored = localStorage.getItem(LOCAL_STORAGE_KEY),
  7. [state, setState] = createState(
  8. stored ? JSON.parse(stored) : value
  9. );
  10.  
  11. // JSON.stringify creates deps on every iterable field
  12. createEffect(() =>
  13. localStorage.setItem(
  14. LOCAL_STORAGE_KEY,
  15. JSON.stringify(state)
  16. ));
  17. return [state, setState];
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement