Last active
May 6, 2021 20:12
-
-
Save shirakaba/4bc215a904bc9a04a35b0618d50ddf10 to your computer and use it in GitHub Desktop.
Svelte custom store example
This file contains hidden or 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
import { writable, derived } from 'svelte/store'; | |
function createCountries() { | |
const { subscribe, update } = writable([ | |
{ | |
name: "United Kingdom", | |
weather: "Cold", | |
description: "Nice tea", | |
}, | |
{ | |
name: "France", | |
weather: "Warm", | |
description: "Good access to the United Kingdom", | |
} | |
]) | |
function add(country) { | |
update(items => [...items, country]) | |
} | |
return { | |
subscribe, | |
add, | |
} | |
} | |
export const countries = createCountries(); | |
export const selectedCountryName = writable(''); | |
export const country = derived( | |
[countries, selectedCountryName], | |
([$countries, selectedCountryName]) => $countries.find(c => c.name === selectedCountryName) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From trying to improve upon: