storing a function inside a store

is it currently possible to store a function inside a store? it seems like they are being called immediately when I enter them in a store atm.
const [state, setState] = createStore<{ test?: () => number }>({
test: undefined,
});

const test = () => performance.now();
setState('test', test);
console.log(typeof test === typeof state.test) // logs false
console.log(state.test, test); // logs 373409 () => performance.now()
setTimeout(() => console.log(state.test, test()), 1000) // logs 373409 374409
const [state, setState] = createStore<{ test?: () => number }>({
test: undefined,
});

const test = () => performance.now();
setState('test', test);
console.log(typeof test === typeof state.test) // logs false
console.log(state.test, test); // logs 373409 () => performance.now()
setTimeout(() => console.log(state.test, test()), 1000) // logs 373409 374409
https://playground.solidjs.com/anonymous/a8dcab94-f01e-48f0-9959-432cc6152de7
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
6 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
bigmistqke
bigmistqkeOP3y ago
it seems to be solved easily by doing setState('test', () => test); 🖇️ I thought I would get type-errors, but ts doesn't complain aa realize now why they are called immediately, because a function is also used as a storesetter.
thetarnav
thetarnav3y ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
thetarnav
thetarnav3y ago
too late, dammit
bigmistqke
bigmistqkeOP3y ago
still learned a thing: didnt know how to do optional function call before
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server