[go: up one dir, main page]

0% found this document useful (0 votes)
58 views1 page

Using Custom Svelte Stores

This document discusses creating custom stores in Svelte. It explains that as long as an object implements the subscribe method, it is a store. Custom stores can include domain-specific logic. As an example, it shows how a count store could include increment, decrement, and reset methods instead of exposing set and update directly. This avoids exposing the internal implementation details of the store.

Uploaded by

howesteve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views1 page

Using Custom Svelte Stores

This document discusses creating custom stores in Svelte. It explains that as long as an object implements the subscribe method, it is a store. Custom stores can include domain-specific logic. As an example, it shows how a count store could include increment, decrement, and reset methods instead of exposing set and update directly. This avoids exposing the internal implementation details of the store.

Uploaded by

howesteve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

5/22/23, 10:38 PM Stores / Custom stores • Svelte Tutorial

Stores / Custom stores

As long as an object correctly implements the subscribe method, it's a store. Beyond
that, anything goes. It's very easy, therefore, to create custom stores with domain-
specific logic.

For example, the count store from our earlier example could include increment ,
decrement and reset methods and avoid exposing set and update :

function createCount() {
const { subscribe, set, update } = writable(0);

return {
subscribe,
increment: () => update(n => n + 1),
decrement: () => update(n => n - 1),
reset: () => set(0)
};
}

Show me Next

Edit this chapter

https://svelte.dev/tutorial/custom-stores 1/1

You might also like