Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get store value from another store?

How to get store value from another store? https://svelte.dev/repl/0ab80c2fb8e045958d844bd4b11c04a9?version=3.22.1

In the example I include a variable inputVal in stores.js file and changing in
set: (val) => {inputVal=val; set( val );}, and use in fn setToZero

Question: how to do it directly without using the inputVal variable?

like image 798
lukaszpolowczyk Avatar asked Dec 05 '25 09:12

lukaszpolowczyk


1 Answers

Use get(store) to access a stores value.

Example:

import {get, writable} from 'svelte/store'

const myStore = writable(41)

const value = get(myStore)

If you are accessing a store from within a .svelte component, there is a shortcut. Just prefix the store name with $, eg. const value = $myStore.

like image 50
joshnuss Avatar answered Dec 08 '25 06:12

joshnuss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!