Function leptos::create_isomorphic_effect

source ยท
pub fn create_isomorphic_effect<T>(
    f: impl Fn(Option<T>) -> T + 'static
) -> Effect<T>
where T: 'static,
Expand description

Creates an effect; unlike effects created by create_effect, isomorphic effects will run on the server as well as the client.

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

// โœ… use effects to interact between reactive state and the outside world
create_isomorphic_effect(move |_| {
  // immediately prints "Value: 0" and subscribes to `a`
  log::debug!("Value: {}", a.get());
});

set_a.set(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_isomorphic_effect(move |_| {
  // this technically works but can cause unnecessary re-renders
  // and easily lead to problems like infinite loops
  set_b.set(a.get() + 1);
});