From a27163fc5fa242e0113ce5ad3d6939afd136f0d6 Mon Sep 17 00:00:00 2001 From: Andre Wiggins Date: Tue, 7 Dec 2021 16:10:15 -0800 Subject: [PATCH 1/2] Add test for useEffect ordering with useMemo --- hooks/test/browser/combinations.test.js | 46 ++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/hooks/test/browser/combinations.test.js b/hooks/test/browser/combinations.test.js index 986533c2a6..ea8d26ff11 100644 --- a/hooks/test/browser/combinations.test.js +++ b/hooks/test/browser/combinations.test.js @@ -6,7 +6,8 @@ import { useReducer, useEffect, useLayoutEffect, - useRef + useRef, + useMemo } from 'preact/hooks'; import { scheduleEffectAssert } from '../_util/useEffectUtil'; @@ -298,4 +299,47 @@ describe('combinations', () => { 'effect outer call hello 2' ]); }); + + it('should run effects child-first even for children separated by memoization', () => { + let ops = []; + + /** @type {() => void} */ + let updateChild; + /** @type {() => void} */ + let updateParent; + + function Child() { + const [_, setCount] = useState(0); + updateChild = () => setCount(c => c + 1); + useEffect(() => { + ops.push('child effect'); + }); + return
Child
; + } + + function Parent() { + const [_, setCount] = useState(0); + updateParent = () => setCount(c => c + 1); + const memoedChild = useMemo(() => , []); + useEffect(() => { + ops.push('parent effect'); + }); + return ( +
+
Parent
+ {memoedChild} +
+ ); + } + + act(() => render(, scratch)); + expect(ops).to.deep.equal(['child effect', 'parent effect']); + + ops = []; + updateChild(); + updateParent(); + act(() => rerender()); + + expect(ops).to.deep.equal(['child effect', 'parent effect']); + }); }); From 0a43672f84b47b8d4db88495820880b755b2c5cf Mon Sep 17 00:00:00 2001 From: Andre Wiggins <459878+andrewiggins@users.noreply.github.com> Date: Tue, 7 Dec 2021 16:25:36 -0800 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Jovi De Croock --- hooks/test/browser/combinations.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hooks/test/browser/combinations.test.js b/hooks/test/browser/combinations.test.js index ea8d26ff11..ef46d713c4 100644 --- a/hooks/test/browser/combinations.test.js +++ b/hooks/test/browser/combinations.test.js @@ -309,7 +309,7 @@ describe('combinations', () => { let updateParent; function Child() { - const [_, setCount] = useState(0); + const [, setCount] = useState(0); updateChild = () => setCount(c => c + 1); useEffect(() => { ops.push('child effect'); @@ -318,7 +318,7 @@ describe('combinations', () => { } function Parent() { - const [_, setCount] = useState(0); + const [, setCount] = useState(0); updateParent = () => setCount(c => c + 1); const memoedChild = useMemo(() => , []); useEffect(() => {