Struct sycamore_reactive::Signal[][src]

pub struct Signal<T: 'static> { /* fields omitted */ }
Expand description

State that can be set.

Example

use sycamore_reactive::*;

let state = Signal::new(0);
assert_eq!(*state.get(), 0);

state.set(1);
assert_eq!(*state.get(), 1);

Implementations

Creates a new signal with the given value.

Example
let state = Signal::new(0);

Set the current value of the state.

This will notify and update any effects and memos that depend on this value.

Example

let state = Signal::new(0);
assert_eq!(*state.get(), 0);

state.set(1);
assert_eq!(*state.get(), 1);

Get the StateHandle associated with this signal.

This is a shortcut for (*signal).clone().

Consumes this signal and returns its underlying StateHandle.

Calls all the subscribers without modifying the state. This can be useful when using patterns such as inner mutability where the state updated will not be automatically triggered. In the general case, however, it is preferable to use Signal::set instead.

Methods from Deref<Target = StateHandle<T>>

Get the current value of the state. When called inside a reactive scope, calling this will add itself to the scope’s dependencies.

Example
use sycamore_reactive::*;

let state = Signal::new(0);
assert_eq!(*state.get(), 0);

state.set(1);
assert_eq!(*state.get(), 1);

Get the current value of the state, without tracking this as a dependency if inside a reactive context.

Example
use sycamore_reactive::*;

let state = Signal::new(1);

let double = create_memo({
    let state = state.clone();
    move || *state.get_untracked() * 2
});

assert_eq!(*double.get(), 2);

state.set(2);
// double value should still be old value because state was untracked
assert_eq!(*double.get(), 2);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

The resulting type after dereferencing.

Dereferences the value.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Compare self to key and return true if they are equal.

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.