Skip to content
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

Internal Svelte State #2

Open
stolinski opened this issue Oct 12, 2021 · 3 comments
Open

Internal Svelte State #2

stolinski opened this issue Oct 12, 2021 · 3 comments

Comments

@stolinski
Copy link
Contributor

Instead of plugging state into Svelte Toy, have it be automatic.

@stolinski
Copy link
Contributor Author

If anyone out there (in the universe) has ideas for this, would love more info on how to make this happen.

@snake-345
Copy link

Maybe try to monkey patch svelte/store.
I played with that a little here: Repl

@luist1228
Copy link

@stolinski I know it's kind of annoying but I made a simple store like this:

import { writable } from "svelte/store";

export const stores = writable({})

export function addStore(storeObject) {
  const key = Object.keys(storeObject)[0]
  stores.update((currentStores) => {
    currentStores[key] = storeObject[key]
    return currentStores
  })

}

export function removeStore(storeObject) {
  const key = Object.keys(storeObject)[0]
  stores.update((currentStores) => {
    delete currentStores[key]
    return currentStores
  })

}

All you have to do is whenever you are adding a new store call the addStore({exampleStore}) by passing an object and then to the Toy component you pass the subscription <Toy register={$stores} />

I haven't tried it yet because I had trouble with installing the package in my sveltekit project and I'm doing my own implementation, guiding myself from the implementation of levelupTutorials.

Be careful with my implementation because I didn't add a check for if the store is in the Stores object, but feel free to use it and modify it as you need

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants