adamhaile / S

S.js - Simple, Clean, Fast Reactive Programming in Javascript
MIT License
1.31k stars 67 forks source link

Local state inside effect / hooks #42

Open AStaroverov opened 3 years ago

AStaroverov commented 3 years ago

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)
yamiteru commented 2 years ago

Yeah it is, just use let

nonphoto commented 2 years ago

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.