question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Erroneous "Stores must be declared at the top level of the component" error

See original GitHub issue

Describe 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:open
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
Conduitrycommented, Jul 17, 2020

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 where foo was shadowing a top-level foo would suddenly mean something different.) Having a nice long intervening period where it throws a compile time error seems a lot safer.

1reaction
Myrmodcommented, May 5, 2022

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

import { get } from 'svelte/store'

function getValue(store) {
  // works
  return get(store)
  
  // doesn't work
  return $store
}
...

I made an example of my use case in a repl: https://svelte.dev/repl/acccf5911f7e41af8025ee88fc005a56?version=3.48.0

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found