Skip to content

Commit

Permalink
Add a loaded check to useSessionStorage
Browse files Browse the repository at this point in the history
Limit `useSessionStorage` so that `onSnapshot(store, handler)` only runs once.
  • Loading branch information
eatyourgreens committed Mar 21, 2022
1 parent fc7d9e3 commit c241d3f
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/lib-classifier/src/hooks/useSessionStorage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { addDisposer, onSnapshot } from 'mobx-state-tree'
import { useEffect, useState } from 'react'
import { useState } from 'react'

import { asyncSessionStorage } from '@helpers'

Expand All @@ -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
}

0 comments on commit c241d3f

Please sign in to comment.