Skip to content

Commit

Permalink
Make use persistent in-memory state start with correct state & check …
Browse files Browse the repository at this point in the history
…equality before setState
  • Loading branch information
jahooma committed Apr 11, 2023
1 parent df510e1 commit c83e6c8
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions web/hooks/use-persistent-in-memory-state.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { safeJsonParse } from 'common/util/json'
import { useEffect, useState } from 'react'
import { useEffect } from 'react'
import { useStateCheckEquality } from './use-state-check-equality'

const store: { [key: string]: any } = {}

export const usePersistentInMemoryState = <T>(initialValue: T, key: string) => {
const [state, setState] = useState<T>(initialValue)
const [state, setState] = useStateCheckEquality<T>(
safeJsonParse(store[key]) ?? initialValue
)

useEffect(() => {
const storedValue = safeJsonParse(store[key]) ?? initialValue
Expand Down

0 comments on commit c83e6c8

Please sign in to comment.