gornvix
gornvix

Reputation: 3384

Error when reading all data from a Svelte store

I can't read all data from my writable store:

Error: 'store' is not exported by src\stores.js, imported by src\components\Users.svelte

The store:

import {subscribe} from 'svelte/internal'
import {writable} from 'svelte/store'
export const fooStore = (key, initial) => {
  const foo = localStorage.getItem(key)
  const data = foo ? JSON.parse(user) : initial
  const store = writable(data, () => {
    const unsubscribe = store.subscribe(value => {
      localStorage.setItem(key, JSON.stringify(value))
    })
    return unsubscribe
  })
  return store
}

The view:

<script>
  import {store} from '../stores.js'
</script>

<table>
  <thead></thead>
  <tbody>
    {#each $store as foo}
      <tr>
        <td>foo.bar</td>
        <td>foo.qaz</td>
      </tr>
    {/each}
  </tbody>
</table>

Upvotes: 0

Views: 220

Answers (1)

Stephane Vanraes
Stephane Vanraes

Reputation: 16451

The only thing you are exporting from your store file is a function called fooStore.

When you do import { store } from ... it will look for a named export in that file with the name of store, so it would expect the file to somewhere have export const store = ... or similar.

Either you declare some stores in the store file, if you want you can create them with that fooStore function:


export const fooStore = () => {}

// with fooStore
export const myStore1 = fooStore();

// plain store
export const myStore2 = writable(123);

or you import the function in your component and declare the store there

<script>
  import { fooStore } from '...'
  const store = fooStore(...)
</script>

Upvotes: 1

Related Questions