# svelte/require-store-reactive-access
disallow to use of the store itself as an operand. Need to use $ prefix or get function.
- ⚙️ This rule is included in
"plugin:svelte/recommended". - 🔧 The
--fixoption on the command line can automatically fix some of the problems reported by this rule.
# 📖 Rule Details
This rule disallow to use of the store itself as an operand.
You should access the store value using the $ prefix or the get function.
<script>
/* eslint svelte/require-store-reactive-access: "error" */
import { writable, get } from 'svelte/store';
const storeValue = writable('world');
const color = writable('red');
/* ✓ GOOD */
$: message = `Hello ${$storeValue}`;
/* ✗ BAD */
$: message = `Hello ${Use the $ prefix or the get function to access reactive values instead of accessing the raw store. (svelte/require-store-reactive-access)storeValue}`;
</script>
<!-- ✓ GOOD -->
<p>{$storeValue}</p>
<p>{get(storeValue)}</p>
<p class={$storeValue} />
<p style:color={$color} />
<MyComponent prop="Hello {$storeValue}" />
<MyComponent bind:this={$storeValue} />
<MyComponent --style-props={$storeValue} />
<MyComponent {...$storeValue} />
<!-- ✗ BAD -->
<p>{Use the $ prefix or the get function to access reactive values instead of accessing the raw store. (svelte/require-store-reactive-access)storeValue}</p>
<p class={Use the $ prefix or the get function to access reactive values instead of accessing the raw store. (svelte/require-store-reactive-access)storeValue} />
<p style:Use the $ prefix or the get function to access reactive values instead of accessing the raw store. (svelte/require-store-reactive-access)color />
<MyComponent prop="Hello {Use the $ prefix or the get function to access reactive values instead of accessing the raw store. (svelte/require-store-reactive-access)storeValue}" />
<MyComponent bind:this={Use the $ prefix or the get function to access reactive values instead of accessing the raw store. (svelte/require-store-reactive-access)storeValue} />
<MyComponent --style-props={Use the $ prefix or the get function to access reactive values instead of accessing the raw store. (svelte/require-store-reactive-access)storeValue} />
<MyComponent {...Use the $ prefix or the get function to access reactive values instead of accessing the raw store. (svelte/require-store-reactive-access)storeValue} />
This rule checks the usage of store variables only if the store can be determined within a single file.
However, when using @typescript-eslint/parser and full type information, this rule uses the type information to determine if the expression is a store.
// fileName: my-stores.ts
import { writable } from 'svelte/store';
export const storeValue = writable('hello');
<script lang="ts">
/* eslint svelte/require-store-reactive-access: "error" */
import { storeValue } from './my-stores';
</script>
<!-- ✓ GOOD -->
<p>{$storeValue}</p>
<!-- ✗ BAD -->
<p>{storeValue}</p>
With type information, this rule also allows the following:
- A store that is also a function (e.g. melt-ui builders are stores and actions) can be called (including tagged templates), and used with
use:,transition:,in:,out:,animate:,on:, and event attributes (e.g.onclick, Svelte 5 only). - A store from a function call (e.g.
getContext('key')), or a variable or parameter initialized with one, can be checked with??or??=when the fallback is also a store (e.g.getContext('key') ?? readable(0)), because the type of the call may not includeundefined. This does not apply towritable(),readable(), andderived(), since they always return a store.
<script lang="ts">
/* eslint svelte/require-store-reactive-access: "error" */
import { getContext } from 'svelte';
import { type Readable, readable } from 'svelte/store';
import { createDropdownMenu } from '@melt-ui/svelte';
const {
elements: { trigger }
} = createDropdownMenu();
/* ✓ GOOD */
function getStore(): Readable<number> {
return getContext('key') ?? readable(0);
}
</script>
<!-- ✓ GOOD -->
<button {...$trigger} use:trigger>open menu</button>
The auto-fix adds the $ prefix only to stores declared at the top level of the component, and not inside the module script (<script module> or <script context="module">), because Svelte does not allow it there.
It also does not fix the left side of ||=, &&=, and ??=, because $store ||= value sets the store value instead.
# 🔧 Options
Nothing.
# 🚀 Version
This rule was introduced in eslint-plugin-svelte v2.12.0