Erroneous "Stores must be declared at the top level of the component" error
See original GitHub issueDescribe the bug Since #5079, valid code is causing the following error:
Stores must be declared at the top level of the component (this may change in a future version of Svelte)
To Reproduce
https://svelte.dev/repl/8240f1807eda4d429018826229910e48?version=3.24.0
It’s coming from this code:
<script>
import { getChartContext } from './Chart.svelte';
import { get_ticks } from '../utils/ticks.mjs';
// ...
const { x1, y1, x2, y2, x, y } = getChartContext();
// ...
$: style = orientation === HORIZONTAL
? (y, i) => `width: 100%; height: 0; top: ${$y(y, i)}%`
: (x, i) => `width: 0; height: 100%; left: ${$x(x, i)}%`;
</script>
The y
that is part of the chart context is a store; the y
inside the closure is a number.
Expected behavior No error
Severity
Moderate. Requires either downgrading Svelte in our project, or working around the bug in Pancake
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Stores must be declared at the top level of the component ...
Erroneous "Stores must be declared at the top level of the component" error · Describe the bug · To Reproduce · Expected behavior...
Read more >Next.js: Error: React.Children.only expected to receive a ...
So there are two child nodes (One is space and another is <a> tag) which is invalid and hence such error occurs.
Read more >Working with Svelte stores - Learn web development | MDN
Note: Stores can be defined and used outside Svelte components, so you can organize them in any way you please.
Read more >How to Use Svelte Stores to Share Data Between ...
Build the demo app. Create the first Svelte component. Open the file called App.svelte. This file is where you will write the code...
Read more >Documentation
Svelte uses the export keyword to mark a variable declaration as a property ... Note that the store must be declared at the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Something else I had mentioned on that other PR - if we ever plan to allow subscribing to stores that are not defined at the top-level, that would have itself technically been a breaking change vs. the old behavior. (Because using
$foo
in a scope wherefoo
was shadowing a top-levelfoo
would suddenly mean something different.) Having a nice long intervening period where it throws a compile time error seems a lot safer.A problem I had was that I wanted to use the values of stores that have been bound by components. The solution for getting the values of the stores is using
I made an example of my use case in a repl: https://svelte.dev/repl/acccf5911f7e41af8025ee88fc005a56?version=3.48.0