Effects To run a callback each time a signal is changed, use the createEffect() function. Any signal used inside will trigger the callback when they're changed. createEffect(() => { console.log(count()); }); An arguments object is fed to the effect callback if needed, which includes the following properties:
  • lastValue - The previous return value of the effect callback. This is potentially helpful for avoiding unnecessary updates. To take advantage of this feature, you must return a value from the callback, and provide an initial value as the second argument to createEffect().
  • destroy - A function that can be called to destroy the effect, preventing it from running again. This is useful if you want to conditionally stop the effect based on some logic.
createEffect(({ lastValue, destroy }) => { const newCategory = category(); // Suppose `handleCategoryChange()` sets up its own effect, leaving this one redundant. if (newCategory !== lastValue) { handleCategoryChange(newCategory); destroy(); // Unsubscribe the effect and exit early return; } // Handle each category differently if (newCategory === 'default') { handleDefaultCategory(); } else { handleCustomCategory(newCategory); } // Return the new category to be used as the lastValue in the next effect run return newCategory; // Pass the initial value to use as the lastValue in the first run }, 'default');