Open dezudas opened 1 year ago
const persistAtom = (key) => ({ setSelf, onSet }) => { setSelf(() => { let data = storage.getString(key); if(data != null){ return JSON.parse(data); }else { return new DefaultValue(); } });
onSet((newValue, _, isReset) => { if (isReset) { storage.delete(key); } else { storage.set(key, JSON.stringify(newValue)); } }); };
How to use this code?
const persistAtom = (key) => ({ setSelf, onSet }) => { setSelf(() => { let data = storage.getString(key); if(data != null){ return JSON.parse(data); }else { return new DefaultValue(); } });
onSet((newValue, _, isReset) => { if (isReset) { storage.delete(key); } else { storage.set(key, JSON.stringify(newValue)); } }); };