Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { createState, createEffect } from 'solid-js';
- const LOCAL_STORAGE_KEY = 'todos-solid';
- function createLocalState(value) {
- // load stored todos on init
- const stored = localStorage.getItem(LOCAL_STORAGE_KEY),
- [state, setState] = createState(
- stored ? JSON.parse(stored) : value
- );
- // JSON.stringify creates deps on every iterable field
- createEffect(() =>
- localStorage.setItem(
- LOCAL_STORAGE_KEY,
- JSON.stringify(state)
- ));
- return [state, setState];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement