Function leptos::create_effect

source ·
pub fn create_effect<T>(cx: Scope, f: impl Fn(Option<T>) -> T + 'static)where
    T: 'static,
Expand description

Effects run a certain chunk of code whenever the signals they depend on change. create_effect immediately runs the given function once, tracks its dependence on any signal values read within it, and reruns the function whenever the value of a dependency changes.

Effects are intended to run side-effects of the system, not to synchronize state within the system. In other words: don’t write to signals within effects. (If you need to define a signal that depends on the value of other signals, use a derived signal or create_memo).

The effect function is called with an argument containing whatever value it returned the last time it ran. On the initial run, this is None.

By default, effects do not run on the server. This means you can call browser-specific APIs within the effect function without causing issues. If you need an effect to run on the server, use create_isomorphic_effect.

let (a, set_a) = create_signal(cx, 0);
let (b, set_b) = create_signal(cx, 0);

// ✅ use effects to interact between reactive state and the outside world
create_effect(cx, move |_| {
  // immediately prints "Value: 0" and subscribes to `a`
  log::debug!("Value: {}", a());
});

set_a(1);
// ✅ because it's subscribed to `a`, the effect reruns and prints "Value: 1"

// ❌ don't use effects to synchronize state within the reactive system
create_effect(cx, move |_| {
  // this technically works but can cause unnecessary re-renders
  // and easily lead to problems like infinite loops
  set_b(a() + 1);
});