Skip to content

disallow-reactive-vars-on-mount #1205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
baseballyama opened this issue Apr 24, 2025 · 0 comments
Open

disallow-reactive-vars-on-mount #1205

baseballyama opened this issue Apr 24, 2025 · 0 comments
Labels
enhancement New feature or request new rule

Comments

@baseballyama
Copy link
Member

Motivation

onMount runs in the component lifecycle, while reactive variables operate within the reactivity lifecycle. Referencing reactive variables inside the component lifecycle can easily lead to bugs.

Description

You cannot access reactive variables inside the onMount function.

Examples

<script>
	import { onMount } from 'svelte';
	let count = $state(0);

	// ✗ BAD
	onMount(() => {
		console.log(count);
	});
</script>

<!-- ✓ GOOD -->
{#if count > 0}
	<p>Count is positive</p>
{/if}

Additional comments

No response

@baseballyama baseballyama added enhancement New feature or request new rule labels Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request new rule
Projects
None yet
Development

No branches or pull requests

1 participant