Hi,
I have recently started to use Svelte.
Is it possible to share data across components.
Basically, I would like to do some background pre-fetch data on the home component and then use that data in other components.
I dont want to pass it through the context.
Almost like a global variable as such.
You can declare a store,set the variables that u want in it and get them in the components where you need it
Svelte stores
1 Like
Thank you for your assistance.
It did send me in the write direction, however, it seems like my store is not being updated inside my components.
store.js
import { writable } from 'svelte/store';
export const teststore = writable("Init Data");
home.svelte
import { onDestroy } from 'svelte';
import { teststore } from '../js/stores.js';
let localstoreData;
const unsubscribe = teststore.subscribe(value => {
localstoreData = value;
});
onDestroy(unsubscribe);
function onPageInit(pageData) {
teststore.update("Updated from Home");
}
Just to follow up.
I had the wrong syntax in my update function
It should be
teststore.update(n => n = "Updated from Home");
2 Likes