Function as signal value

Using the accessor of createSignal<null | (x:any)=>...>() doesn't work as expected. I need to wrap the function in an array or object for it to become callable. What's the purpose of that behavior?
5 Replies
bigmistqke
bigmistqke2w ago
i think ur probably getting tripped up by the Setter the setter also accept a function:setSignal(prev => ...) so if you want to set a function you have to do setSignal(() => (x: any) => ...)
bigmistqke
bigmistqke2w ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
bigmistqke
bigmistqke2w ago
this is handy, for example to prevent infinite loops:
const [count, setCount] = createSignal(0)
createEffect(() => {
...
setCount(count() + 1); // will cause infinite loop, because you describe to a signal you are setting
})

createEffect(() => {
...
setCount(count => count + 1); // will not cause infinite loop
})
const [count, setCount] = createSignal(0)
createEffect(() => {
...
setCount(count() + 1); // will cause infinite loop, because you describe to a signal you are setting
})

createEffect(() => {
...
setCount(count => count + 1); // will not cause infinite loop
})
fewbar
fewbarOP2w ago
thanks!
bigmistqke
bigmistqke2w ago
Ur very welcome!

Did you find this page helpful?