S
SolidJS3mo ago
ojack

Dynamically remove effect

Is it possible to remove an effect created with createEffect()? Or is there a different way to think about dynamically adding and removing effects? I am in the beginning stages of building a UI where the user can dynamically route signal flow + relationships between elements... still working on fully wrapping my head around reactivity, etc!
3 Replies
Dsaquel
Dsaquel3mo ago
Maybe you should take a look at untrack to remove the effect of a signal or a derived signal: https://www.solidjs.com/tutorial/reactivity_untrack or you can use on to only watch the depencies in your first argument. https://docs.solidjs.com/reference/reactive-utilities/on
SolidJS
Solid is a purely reactive library. It was designed from the ground up with a reactive core. It's influenced by reactive principles developed by previous libraries.
JCM
JCM3mo ago
There is probably something triggering if the effect needs to be there or not. Note that in solidjs, the dependency tree from signal is fully dynamic, so assuming you have a signal enableEffect , you can do following
createEffect(() => {
if (enableEffect()) {
createEffect(() => {
console.log("this effect only exists if `enableEffect()` is true", someOtherSignal());
})
}
// when `enableEffect()` is false, the inner effect is not created
})
createEffect(() => {
if (enableEffect()) {
createEffect(() => {
console.log("this effect only exists if `enableEffect()` is true", someOtherSignal());
})
}
// when `enableEffect()` is false, the inner effect is not created
})
Note that for simple logic like that, it's obviously more readable that way, but example above is to illustrate nested effects
createEffect(() => {
if (enableEffet()) {
console.log("this is only executed if `enableEffect()` is true", someOtherSignal());
}
})
createEffect(() => {
if (enableEffet()) {
console.log("this is only executed if `enableEffect()` is true", someOtherSignal());
}
})
ojack
ojack3mo ago
Ahh okay this is helpful!!!.... I am still wrapping my head around nested effects. I am going to try this way and see if it works for my use case.
Want results from more Discord servers?
Add your server
More Posts