From e93789fad32b14f501b294ef23ab0af3f021a4cb Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Mon, 31 May 2021 08:52:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=AD=90=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=20onShow=20=E4=B8=AD=E6=94=B9=E5=8F=98?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E5=AF=BC=E8=87=B4=E7=88=B6=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=20onShow=20=E4=B8=8D=E6=89=A7=E8=A1=8C=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit close #1564 --- .../remax-runtime/src/__tests__/page.test.tsx | 30 +++++++++++++++++++ .../remax-runtime/src/createPageConfig.ts | 3 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/remax-runtime/src/__tests__/page.test.tsx b/packages/remax-runtime/src/__tests__/page.test.tsx index 515a871f0..3664b16ac 100644 --- a/packages/remax-runtime/src/__tests__/page.test.tsx +++ b/packages/remax-runtime/src/__tests__/page.test.tsx @@ -236,6 +236,36 @@ describe('page', () => { page.shareAppMessage(); expect(log).toEqual(['onShow', 'onShareAppMessage']); }); + + it('call once with child hook', () => { + const log: string[] = []; + const foo = React.createRef(); + + const Child = () => { + const [count, setCount] = React.useState(0); + usePageEvent('onShow', () => { + log.push('child onShow'); + setCount(count + 1); + }); + + return Child; + }; + + const Foo = React.forwardRef((props, ref) => { + usePageEvent('onShow', () => { + log.push('foo onShow'); + }); + + return ( + + + + ); + }); + const page = Page(createPageConfig(() => , TEST_PAGE)); + page.load(); + expect(log).toEqual(['child onShow', 'foo onShow']); + }); }); it('lifecycle methods', () => { diff --git a/packages/remax-runtime/src/createPageConfig.ts b/packages/remax-runtime/src/createPageConfig.ts index a10812541..238c42c5a 100644 --- a/packages/remax-runtime/src/createPageConfig.ts +++ b/packages/remax-runtime/src/createPageConfig.ts @@ -108,7 +108,8 @@ export default function createPageConfig(Page: React.ComponentType, name: s callLifecycle(lifecycle: Lifecycle, ...args: any[]) { const callbacks = this.lifecycleCallback[lifecycle] || []; let result; - callbacks.forEach((callback: any) => { + // 生命周期中可能改变 state 导致 callbacks 发生变化 + [...callbacks].map((callback: any) => { result = callback(...args); }); if (result) {