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 forMaybeDyn<T>
for a list of types.
Structs§
- Node
Handle - A handle to a reactive node (signal, memo, effect) that lets you run further tasks in it or manually dispose it.
- Read
Signal - A read-only reactive value.
- Root
Handle - 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§
- Maybe
Dyn - Represents a value that can be either static or dynamic.
Traits§
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 todispose
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 anotherVec
via a map function. - map_
keyed - Function that maps a
Vec
to anotherVec
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
.