Skip to content

Commit

Permalink
fix: a race condition in useValueRef
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Oct 10, 2019
1 parent e33cbb0 commit bc6004b
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/utils/hooks/useValueRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { useState, useEffect } from 'react'

export function useValueRef<T>(ref: ValueRef<T>) {
const [value, setValue] = useState<T>(ref.value)
useEffect(() => ref.addListener(newValue => setValue(newValue)), [ref])
useEffect(() => {
if (value !== ref.value) setValue(ref.value)
return ref.addListener(v => setValue(v))
}, [ref, value])
return value
}

0 comments on commit bc6004b

Please sign in to comment.