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

A signal that is not bound to a Scope.

Sometimes, it is useful to have a signal that can escape the enclosing reactive scope. However, this cannot be achieved simply with Scope::create_signal because the resulting Signal is tied to the Scope by it’s lifetime. The Signal can only live as long as the Scope.

With RcSignal on the other hand, the lifetime is not tied to a Scope. Memory is managed using a reference-counted smart pointer (Rc). What this means is that RcSignal cannot implement the Copy trait and therefore needs to be manually cloned into all closures where it is used.

In general, Scope::create_signal should be preferred, both for performance and ergonomics.

Usage

To create a RcSignal, use the create_rc_signal function.

Example

let mut outer = None;

create_scope_immediate(|ctx| {
// Even though the RcSignal is created inside a reactive scope, it can escape out of it.
let rc_state = create_rc_signal(0);
let rc_state_cloned = rc_state.clone();
let double = ctx.create_memo(move || *rc_state_cloned.get() * 2);
assert_eq!(*double.get(), 0);

rc_state.set(1);
assert_eq!(*double.get(), 2);

// This isn't possible with simply ctx.create_signal()
outer = Some(rc_state);
});

Methods from Deref<Target = Signal<T>>

Set the current value of the state.

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

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

state.set(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.

Split a signal into getter and setter handles.

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

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

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 = ctx.create_signal(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 = ctx.create_signal(1);
let double = ctx.create_memo(|| *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 = ctx.create_signal(1);
let double = state.map(&ctx, |&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

Call the ReadSignal::track method.

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.

Formats the value using the given formatter. Read more

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)

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

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.