Skip to content

Commit ad6fc5a

Browse files
committed
fix: useStableMemo infinite loop
1 parent 66c7af4 commit ad6fc5a

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

src/useStableMemo.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DependencyList, useRef, useEffect } from 'react'
1+
import { DependencyList, useEffect, useRef } from 'react'
22

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

4949
const cache = isValid ? valueRef.current : { deps, result: factory() }
50-
51-
useEffect(() => {
52-
valueRef.current = cache
53-
})
50+
// must update immediately so any sync renders here don't cause an infinite loop
51+
valueRef.current = cache
5452

5553
return cache.result
5654
}

0 commit comments

Comments
 (0)