From 2ec09647e6bdd5db15ee7b99b1eaa4e2a2ede2be Mon Sep 17 00:00:00 2001 From: Jim O'Donnell Date: Fri, 18 Mar 2022 10:44:11 +0000 Subject: [PATCH] Add a loaded check to useSessionStorage Limit `useSessionStorage` so that `onSnapshot(store, handler)` only runs once. --- .../lib-classifier/src/hooks/useSessionStorage.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/lib-classifier/src/hooks/useSessionStorage.js b/packages/lib-classifier/src/hooks/useSessionStorage.js index 7fd10db9374..e7c08944234 100644 --- a/packages/lib-classifier/src/hooks/useSessionStorage.js +++ b/packages/lib-classifier/src/hooks/useSessionStorage.js @@ -1,5 +1,5 @@ import { addDisposer, onSnapshot } from 'mobx-state-tree' -import { useEffect, useState } from 'react' +import { useState } from 'react' import { asyncSessionStorage } from '@helpers' @@ -13,8 +13,13 @@ function persist(storageKey, store, storage) { } export default function useSessionStorage(enableStorage, store, storageKey) { - if (!!store && enableStorage) { - persist(storageKey, store, asyncSessionStorage) + const [loaded, setLoaded] = useState(false) + + if (!loaded && !!store) { + if (enableStorage) { + persist(storageKey, store, asyncSessionStorage) + } + setLoaded(true) } return store } \ No newline at end of file