Skip to content

Latest commit

ย 

History

History
76 lines (63 loc) ยท 1.4 KB

02_Store.md

File metadata and controls

76 lines (63 loc) ยท 1.4 KB

Store

์ค‘์•™์— ์ €์žฅ์†Œ๋ฅผ ๋‘๊ณ  ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„ or ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€์ง€ ์•Š์€ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ƒํƒœ์— ์ ‘๊ทผ & ๊ณต์œ 

writable

import {writable} from 'svelte/store';

const storeObject = writable(value: any);

//
Object
set: ฦ’ (new_value)
subscribe: ฦ’ subscribe(run, invalidate = noop)
update: ฦ’ update(fn)
[[Prototype]]: Object
//

set

import {writable} from 'svelte/store';

const count = writable(0);  // 0
count.set(1);   // 1
$count = 2; // 2

subscribe

store = { subscribe: (subscription: (value: any) => void) => (() => void), set?: value: any) => void }

readable

์™ธ๋ถ€์—์„œ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋Š” ์ƒํƒœ๊ฐ์ฒด
store = readable(value: any, (set: (value: any) => void) => () => void)
import {readable} from 'svelte/store';

const name = readable('yeon');
const age = readable(26, (set) => {
    //
});

derived

ํŒŒ์ƒ ์Šคํ† ์–ด๋„ ์ƒํƒœ๊ฐ์ฒด ๋ฐ˜ํ™˜
store = derived(a, callback: (a: any) => any)
import {writable, derived} from 'svelte/store';

const count = writable(100);    // 100
const doubleCount = derived(count, $count => $count*2); // 200

get

์ƒํƒœ๊ฐ์ฒด์—์„œ ์ƒํƒœ๊ฐ’์„ ๊ฐ€์ ธ์˜ด
import {writable, get} from 'svelte/store';

const count = writable(10);
const getCount = get(count);    // 10
// count๊ฐ’์„ ๋ฐ”๊ฟ”๋„ getCount๊ฐ’์—๋Š” ์ด์ „ ๊ฐ’