sycamore::reactive

Function create_effect_initial

pub fn create_effect_initial<T>(
    initial: impl FnOnce() -> (Box<dyn FnMut()>, T) + 'static,
) -> T
where T: 'static,
Expand description

Creates an effect that runs a different code path on the first run.

The initial function is expected to return a tuple containing a function for subsequent runs and an optional value that will be returned by the effect.

ยงExample

let state = create_signal(0);

let initial_value = create_effect_initial(move || {
    state.set(100);
    (
        Box::new(move || state.set(state.get() + 1)),
        state.get(), // This value will be returned and assigned to `initial_value`.
    )
});

Note that the initial function is also called within the effect scope. This means that signals created within the initial function will no longer be alive in subsequet runs. If you want to create signals that are alive in subsequent runs, you should use use_current_scope and NodeHandle::run_in.