1

I've declared an effect, something on the lines of:

createEffect(async ()=>{
    let localvar = 0;
    if(storeGetter.var1 && storeGetter.var2){
        while(storeGetter.var3){
            localvar = await storeGetter.someFunction1(storeSetter,storeGetter);
        }
    } 
}

And then in store.someFunction I've declared:

someFunction: async function(setter,getter){
    let localVar2 = 0;
    let ans = 0;
    if(getter.var_4){
         /*do some calculations*/
    }
    return ans;
}

The thing is that when store_var4 is updated, the way solidjs resolves the reactivity is taking it into account as a dependency on the effect.

Is there any possibility of avoiding the triggering of the effect in this case?

I'm not sure if untrack is the way to go or how to use it in this case

Typo
  • 1,875
  • 1
  • 19
  • 32

1 Answers1

0

There are many ways to ignore or selectively trigger effects, using untrack, using conditional statements, passing controlled equals value to createSignal and using on function are the most common ones.

How to listen to only a certain value of an object in solid-js?

However your intent is not clear and there are few problems with your code.

First and foremost, Solid uses synchronous API, using async functions clearly serves no purpose and makes your code very hard reason about.

The way you interact with localstorage has problems, i.e why do you need a while statement? It is a blocking statement and a huge no. If you code would work the way you intended, the UI would be either frozen or janky.

You can find some examples on how to use localstorage here:

snnsnn
  • 10,486
  • 4
  • 39
  • 44
  • hi, there was an error in the example, is an if(getter.var) that's triggering the effect when is updated somewhere else, I'm not sure if changes in someway your answer – Typo Feb 09 '23 at 11:27
  • @Typo It does not make any difference. 1. Your intent is not clear, which means your code is not clear. 2. You have some twisted logic to interact with local storage. 3. While is a blocking statement and a big no. If you need a listener, use a listener. If you need to tap into update cycle, use onMount and onCleanup hooks. – snnsnn Feb 09 '23 at 11:55
  • thank you, I'm learning this framework on the go, I'll try to improve with time :-) – Typo Feb 09 '23 at 12:22
  • @Typo Happy to help. Don't get discouraged and move onto other libraries like React. I have extensive experience with many libraries. Solid is the best and easiest to work with. Good luck. – snnsnn Feb 09 '23 at 14:21