Function leptos::create_trigger

source ยท
pub fn create_trigger() -> Trigger
Expand description

Creates a Trigger, a kind of reactive primitive.

A trigger is a data-less signal with the sole purpose of notifying other reactive code of a change. This can be useful for when using external data not stored in signals, for example.

use std::{cell::RefCell, fmt::Write, rc::Rc};

let external_data = Rc::new(RefCell::new(1));
let output = Rc::new(RefCell::new(String::new()));

let rerun_on_data = create_trigger();

let o = output.clone();
let e = external_data.clone();
create_effect(move |_| {
    // can be `rerun_on_data()` on nightly
    rerun_on_data.track();
    write!(o.borrow_mut(), "{}", *e.borrow());
    *e.borrow_mut() += 1;
});
assert_eq!(*output.borrow(), "1");

rerun_on_data.notify(); // reruns the above effect

assert_eq!(*output.borrow(), "12");