Skip to content

Commit 41fa2a4

Browse files
committed
fix: ensure onMount correctly fires when used externally
1 parent 4c6255f commit 41fa2a4

File tree

5 files changed

+28
-8
lines changed

5 files changed

+28
-8
lines changed

.changeset/early-hounds-float.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: ensure onMount correctly fires when used externally

packages/svelte/src/index-client.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,10 @@ export function onMount(fn) {
2525
lifecycle_outside_component('onMount');
2626
}
2727

28-
if (component_context.l !== null) {
29-
init_update_callbacks(component_context).m.push(fn);
30-
} else {
31-
user_effect(() => {
32-
const cleanup = untrack(fn);
33-
if (typeof cleanup === 'function') return /** @type {() => void} */ (cleanup);
34-
});
35-
}
28+
user_effect(() => {
29+
const cleanup = untrack(fn);
30+
if (typeof cleanup === 'function') return /** @type {() => void} */ (cleanup);
31+
});
3632
}
3733

3834
/**
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
async test({ assert, logs }) {
5+
assert.deepEqual(logs, ['mounted']);
6+
}
7+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import { MyClass } from "./myClass.js";
3+
4+
new MyClass();
5+
</script>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { onMount } from 'svelte';
2+
3+
export class MyClass {
4+
constructor() {
5+
onMount(() => console.log('mounted'));
6+
}
7+
}

0 commit comments

Comments
 (0)