Function leptos_reactive::create_isomorphic_effect
source · pub fn create_isomorphic_effect<T>(
cx: Scope,
f: impl Fn(Option<T>) -> T + 'static
)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(cx, 0);
let (b, set_b) = create_signal(cx, 0);
// ✅ use effects to interact between reactive state and the outside world
create_isomorphic_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_isomorphic_effect(cx, move |_| {
// this technically works but can cause unnecessary re-renders
// and easily lead to problems like infinite loops
set_b(a() + 1);
});