From 17198f5ed3694f4fbec8a69620b0345a07bbdfbb Mon Sep 17 00:00:00 2001 From: Bart Van Remortele Date: Fri, 16 Apr 2021 12:09:42 +0200 Subject: [PATCH] fix: prevent DefaultValue from being set when the localStorage state does not contain the key. Fixes #26 --- src/index.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index 1285b14..9575670 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { AtomEffect } from 'recoil' +import { AtomEffect, DefaultValue } from 'recoil' export interface PersistStorage { setItem(key: string, value: string): void | Promise @@ -33,7 +33,7 @@ export const recoilPersist = ( if (trigger === 'get') { const state = getState() if (typeof state.then === 'function') { - state.then((s) => { + state.then((s: any) => { if (s.hasOwnProperty(node.key)) { setSelf(s[node.key]) } @@ -44,15 +44,18 @@ export const recoilPersist = ( } } - onSet((newValue) => { - const state = getState() - - if (newValue instanceof DefaultValue) { - if(state.hasOwnProperty(node.key)) delete state[node.key]; + onSet(async (newValue) => { + const state = await getState() + if ( + newValue !== null && + newValue !== undefined && + newValue instanceof DefaultValue + ) { + if (state.hasOwnProperty(node.key)) delete state[node.key] } else { state[node.key] = newValue } - + setState(state) }) } @@ -73,6 +76,9 @@ export const recoilPersist = ( } const parseState = (state: string) => { + if (state === undefined) { + return {} + } try { return JSON.parse(state) } catch (e) {