Skip to content

Commit

Permalink
fix: useStableMemo infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Sep 6, 2019
1 parent 66c7af4 commit ad6fc5a
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/useStableMemo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DependencyList, useRef, useEffect } from 'react'
import { DependencyList, useEffect, useRef } from 'react'

function isEqual(a: DependencyList, b: DependencyList) {
if (a.length !== b.length) return false
Expand Down Expand Up @@ -47,10 +47,8 @@ export default function useStableMemo<T>(
}

const cache = isValid ? valueRef.current : { deps, result: factory() }

useEffect(() => {
valueRef.current = cache
})
// must update immediately so any sync renders here don't cause an infinite loop
valueRef.current = cache

return cache.result
}

0 comments on commit ad6fc5a

Please sign in to comment.