Last active
March 28, 2023 12:46
-
-
Save joaquimnetocel/1820261df55ca76da6c000defdff7bfe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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