facebookexperimental / Recoil

Recoil is an experimental state management library for React apps. It provides several capabilities that are difficult to achieve with React alone, while being compatible with the newest features of React.
https://recoiljs.org/
MIT License
19.58k stars 1.18k forks source link

recoil persist using mmkv #2210

Open dezudas opened 1 year ago

dezudas commented 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)); } }); };

vitaliybalayan commented 1 year ago

How to use this code?