diff --git a/packages/solid/src/reactive/signal.ts b/packages/solid/src/reactive/signal.ts index 121c44ea0..19ddbb3bc 100644 --- a/packages/solid/src/reactive/signal.ts +++ b/packages/solid/src/reactive/signal.ts @@ -963,12 +963,15 @@ export function on( /** * Runs an effect only after initial render on mount - * @param fn an effect that should run only once on mount + * @param fn an effect that should run only once on mount. It can return a function to be run on cleanup. * * @description https://docs.solidjs.com/reference/lifecycle/on-mount */ -export function onMount(fn: () => void) { - createEffect(() => untrack(fn)); +export function onMount(fn: () => (void | (() => void))) { + createEffect(() => untrack(() => { + const toRunOnCleanup = fn(); + if (toRunOnCleanup) onCleanup(toRunOnCleanup); + })); } /**