Skip to content

Commit 1af2920

Browse files
authored
Merge pull request #4807 from easyops-cn/williamcai/brick-kit/BrickAsComponent/dispatch_mount_event_after_element_of_template_brick_is_assigned
fix(BrickAsComponent): dispatch mount event after element of template…
2 parents a8efbe1 + 8822833 commit 1af2920

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

packages/brick-kit/src/BrickAsComponent.spec.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@ describe("BrickAsComponent", () => {
789789
const wrapper = mount(<BrickAsComponent useBrick={useBrick} />);
790790

791791
await (global as any).flushPromises();
792+
jest.runAllTimers();
792793
onMountHandlers.map((handler) => {
793794
expect(spiedListenerFactory).toBeCalledWith(
794795
handler,

packages/brick-kit/src/BrickAsComponent.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,14 @@ export function SingleBrickAsComponentFactory(React: typeof _React) {
405405

406406
const onMount = useBrick.lifeCycle?.onMount;
407407
if (onMount) {
408-
dispatchLifeCycleEvent(
409-
new CustomEvent("mount"),
410-
transformEvents(data, { onMount }).onMount,
411-
brick
412-
);
408+
// wait for element of template brick to be assigned
409+
queueMicrotask(() => {
410+
dispatchLifeCycleEvent(
411+
new CustomEvent("mount"),
412+
transformEvents(data, { onMount }).onMount,
413+
brick
414+
);
415+
});
413416
}
414417

415418
const onScrollIntoView = useBrick.lifeCycle?.onScrollIntoView;

0 commit comments

Comments
 (0)