pub struct Signal<T>(_);
Expand description

A container of reactive state that can be updated and subscribed to.

Example

Creating a Signal requires a reactive Scope. Generally, you can use the cx parameter obtained from your component or from inside a create_effect_scoped.

let signal = create_signal(cx, 123);    // A signal of type `i32`.
let signal = create_signal(cx, true);   // A signal of type `bool`.
let signal = create_signal(cx, "abc");  // A signal of type `&str`.

Implementations

Set the current value of the state.

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

Example
let state = create_signal(cx, 0);
assert_eq!(*state.get(), 0);

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

Set the current value of the state wrapped in a Rc. Unlike Signal::set(), this method accepts the value wrapped in a Rc because the underlying storage is already using Rc, thus preventing an unnecessary clone.

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

Example
let state = create_signal(cx, 0);
assert_eq!(*state.get(), 0);

state.set_rc(Rc::new(1));
assert_eq!(*state.get(), 1);

Set the current value of the state without triggering subscribers.

Make sure you know what you are doing because this can make state inconsistent.

Set the current value of the state wrapped in a Rc without triggering subscribers.

See the documentation for Signal::set_rc() for more information.

Make sure you know what you are doing because this can make state inconsistent.

Split a signal into getter and setter handles.

Example
let (state, set_state) = create_signal(cx, 0).split();
assert_eq!(*state(), 0);

set_state(1);
assert_eq!(*state(), 1);

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.

This will also re-compute all the subscribers of this signal by calling all the dependency callbacks.

Return a mutable handle to make it easier to mutate the inner value. This requires the inner type to implement Clone.

Example
let state = create_signal(cx, "Hello ".to_string());
state.modify().push_str("World!");
assert_eq!(*state.get(), "Hello World!");

Take the current value out and replace it with the default value.

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

Take the current value out and replace it with the default value without triggering subscribers.

Make sure you know what you are doing because this can make state inconsistent.

Methods from Deref<Target = ReadSignal<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
let state = create_signal(cx, 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
let state = create_signal(cx, 1);
let double = create_memo(cx, || *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);

Creates a mapped ReadSignal. This is equivalent to using create_memo.

Example
let state = create_signal(cx, 1);
let double = state.map(cx, |&x| x * 2);
assert_eq!(*double.get(), 2);

state.set(2);
assert_eq!(*double.get(), 4);

When called inside a reactive scope, calling this will add itself to the scope’s dependencies.

To both track and get the value of the signal, use ReadSignal::get instead.

Trait Implementations

Performs the += operation. Read more

Call the ReadSignal::track method.

Formats the value using the given formatter. Read more

The resulting type after dereferencing.

Dereferences the value.

Formats the value using the given formatter. Read more

Performs the /= operation. Read more

Feeds this value into the given Hasher. Read more

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

Performs the *= operation. Read more

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Performs the -= operation. Read more

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.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Converts the given value to a String. 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.