Skip to content

Latest commit

 

History

History
24 lines (21 loc) · 517 Bytes

WRAPPER_RECOIL.md

File metadata and controls

24 lines (21 loc) · 517 Bytes

recoil storage wrapper

If you want to use MMKV with recoil, Use the following persist 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));
    }
  });
};