Crate sycamore_reactive

Source
Expand description

Reactive primitives for Sycamore.

use sycamore_reactive::*;

create_root(|| {
    let greeting = create_signal("Hello");
    let name = create_signal("World");

    let display_text = create_memo(move || format!("{greeting} {name}!"));
    assert_eq!(display_text.get_clone(), "Hello World!");

    name.set("Sycamore");
    assert_eq!(display_text.get_clone(), "Hello Sycamore!");
});

§A note on nightly

If you are using rust nightly, you can enable the nightly feature to enable the more terse syntax for signal get/set.

let signal = create_signal(123);

// Stable:
let value = signal.get();
signal.set(456);

// Nightly:
let value = signal();
signal(456);

Of course, the stable .get() also works on nightly as well if that’s what you prefer.

Macros§

impl_into_maybe_dyn
A macro that makes it easy to write implementations for Into<MaybeDyn<T>>.
impl_into_maybe_dyn_with_convert
Create From<U> implementations for MaybeDyn<T> for a list of types.

Structs§

NodeHandle
A handle to a reactive node (signal, memo, effect) that lets you run further tasks in it or manually dispose it.
ReadSignal
A read-only reactive value.
RootHandle
A handle to a root. This lets you reinitialize or dispose the root for resource cleanup.
Signal
A reactive value that can be read and written to.

Enums§

MaybeDyn
Represents a value that can be either static or dynamic.

Traits§

Trackable
A trait that is implemented for reactive data that can be tracked, such as Signal.

Functions§

batch
Batch updates from related signals together and only run memos and effects at the end of the scope.
create_child_scope
Create a child scope.
create_effect
Creates an effect on signals used inside the effect closure.
create_effect_initial
Creates an effect that runs a different code path on the first run.
create_memo
Creates a memoized computation from some signals.
create_reducer
An alternative to create_signal that uses a reducer to get the next value.
create_root
Creates a new reactive root with a top-level reactive node. The returned RootHandle can be used to dispose the root.
create_selector
Creates a memoized value from some signals.
create_selector_with
Creates a memoized value from some signals. Unlike create_memo, this function will not notify dependents of a change if the output is the same.
create_signal
Create a new Signal.
map_indexed
Function that maps a Vec to another Vec via a map function.
map_keyed
Function that maps a Vec to another Vec via a map function and a key.
on
A helper function for making dependencies explicit.
on_cleanup
Adds a callback that is called when the scope is destroyed.
provide_context
Provide a context value in this scope.
provide_context_in_new_scope
Provide a context value in a new scope.
try_use_context
Tries to get a context value of the given type. If no context is found, returns None.
untrack
Run the passed closure inside an untracked dependency scope.
use_context
Get a context with the given type. If no context is found, this panics.
use_context_or_else
Try to get a context with the given type. If no context is found, returns the value of the function and sets the value of the context in the current scope.
use_current_scope
Get a handle to the current reactive scope.
use_global_scope
Get a handle to the root reactive scope.
use_scope_depth
Gets how deep the current scope is from the root/global scope. The value for the global scope itself is always 0.