S icon indicating copy to clipboard operation
S copied to clipboard

Local state inside effect / hooks

Open AStaroverov opened this issue 3 years ago • 2 comments

I have a question. It's a bad idea for S to create something like a local state for effects (hooks?)? For example

const $source = S.value(0);
const $target = S.value(0);

$.effect(() => {
  const $throttler = S.localValue(0); 
  const throttler = S.sample($throttler);
  const source = $source();

  $throttler(throttler + 1);
  
  if (throttler % 3 === 0) {
    $target(source);
  }
})

$source($source() + 1)
$source($source() + 1)
$source($source() + 1)

AStaroverov avatar Feb 15 '21 20:02 AStaroverov

Yeah it is, just use let

yamiteru avatar Aug 16 '22 00:08 yamiteru

You're free to create signals inside effects (or effects inside effects) and this can sometimes be useful. In this particular example though, like @yamiteru said, you can use a regular variable instead of a signal.

nonphoto avatar Aug 16 '22 19:08 nonphoto