milomg / reactively

MIT License
415 stars 23 forks source link

Is there a replacement for "effect"? #15

Open cwayfinder opened 1 year ago

cwayfinder commented 1 year ago

How to run a function whenever one or more dependency values change?

For example, we have the following code

// declare some reactive variables.
const counter = reactive(0);
const isEven = reactive(() => (counter.value & 1) == 0);
const render = reactive(() => {
  document.body.textContent = isEven.value ? "even" : "odd";
});

How to run render() automatically whenever the counter changes?

milomg commented 1 year ago

One way to do that is:

autoStabilize();

// declare some reactive variables.
const counter = reactive(0);
const isEven = reactive(() => (counter.value & 1) == 0);
const render = reactive(() => {
  document.body.textContent = isEven.value ? "even" : "odd";
}, {effect: true});
alijaya commented 1 year ago

What is autoStabilize() and what is {effect:true} ?