Skip to content

Instantly share code, notes, and snippets.

@joaquimnetocel
Last active March 28, 2023 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaquimnetocel/1820261df55ca76da6c000defdff7bfe to your computer and use it in GitHub Desktop.
Save joaquimnetocel/1820261df55ca76da6c000defdff7bfe to your computer and use it in GitHub Desktop.
// store.ts
import { getContext, setContext } from 'svelte';
import { writable, type Writable } from 'svelte/store';
export function createStore() {
const mystore = writable('INITIAL VALUE');
setContext('contextStore', mystore);
}
export function readStore() {
return getContext<Writable<string>>('contextStore');
}
<!-- +layout.svelte -->
<script lang="ts">
import { createStore } from './store';
createStore();
</script>
<slot />
<!-- IN ANY COMPONENT: -->
<script lang="ts">
import { readStore } from './store';
const storeData = readStore();
$storeData = 'NOW YOU CAN CHANGE THE STORE WITHOUT SHARING THE VALUE WITH OTHER USERS.';
</script>
{$storeData}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment