Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
fix: 修复子组件的 onShow 中改变状态导致父组件 onShow 不执行的问题
Browse files Browse the repository at this point in the history
close #1564
  • Loading branch information
yesmeck committed May 31, 2021
1 parent fa54e5a commit e93789f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
30 changes: 30 additions & 0 deletions packages/remax-runtime/src/__tests__/page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>();

const Child = () => {
const [count, setCount] = React.useState(0);
usePageEvent('onShow', () => {
log.push('child onShow');
setCount(count + 1);
});

return <View>Child</View>;
};

const Foo = React.forwardRef((props, ref) => {
usePageEvent('onShow', () => {
log.push('foo onShow');
});

return (
<View>
<Child />
</View>
);
});
const page = Page(createPageConfig(() => <Foo ref={foo} />, TEST_PAGE));
page.load();
expect(log).toEqual(['child onShow', 'foo onShow']);
});
});

it('lifecycle methods', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/remax-runtime/src/createPageConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ export default function createPageConfig(Page: React.ComponentType<any>, 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) {
Expand Down

0 comments on commit e93789f

Please sign in to comment.