From ad6fc5a7f9eb1790142ac4110e182982c0f4490d Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Fri, 6 Sep 2019 16:25:15 -0400 Subject: [PATCH] fix: useStableMemo infinite loop --- src/useStableMemo.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/useStableMemo.ts b/src/useStableMemo.ts index e328510..7d7f176 100644 --- a/src/useStableMemo.ts +++ b/src/useStableMemo.ts @@ -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 @@ -47,10 +47,8 @@ export default function useStableMemo( } 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 }