Skip to content

Commit

Permalink
Fix state update bug
Browse files Browse the repository at this point in the history
Maybe try the context approach later.
  • Loading branch information
bcomnes committed Oct 10, 2022
1 parent b2a1943 commit f2cc267
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions web/hooks/useLSP.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env browser */
import { createContext, useContext } from 'uland-isomorphic'
import { useEffect, useState } from 'uland-isomorphic'
import lsp from 'local-storage-proxy'
import { defaultFrontendFlags } from '../../plugins/flags/frontend-flags.js'

Expand All @@ -19,17 +19,16 @@ if (typeof window !== 'undefined') {
window.state = state
}

const StateContext = createContext()

StateContext.provide(state)

const listener = (ev) => { StateContext.provide(state) }
state.addEventListener('update', listener)

// TODO: look into this: https://usehooks.com/useLocalStorage/

export function useLSP () {
const lsp = useContext(StateContext)
const [lsp, setLSP] = useState(state)
useEffect(() => {
const listener = (ev) => { setLSP(state) }
state.addEventListener('update', listener)

return () => {
state.removeEventListener('update', listener)
}
}, [state])

return lsp
}

0 comments on commit f2cc267

Please sign in to comment.