Struct sycamore_reactive::Signal
source · [−]pub struct Signal<T>(_);
Expand description
Reactive state that can be updated and subscribed to.
Implementations
sourceimpl<T> Signal<T>
impl<T> Signal<T>
sourcepub fn set(&self, value: T)
pub fn set(&self, value: 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 = create_signal(cx, 0);
assert_eq!(*state.get(), 0);
state.set(1);
assert_eq!(*state.get(), 1);
sourcepub fn set_rc(&self, value: Rc<T>)
pub fn set_rc(&self, value: Rc<T>)
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);
sourcepub fn set_silent(&self, value: T)
pub fn set_silent(&self, value: T)
Set the current value of the state without triggering subscribers.
Make sure you know what you are doing because this can make state inconsistent.
sourcepub fn set_rc_silent(&self, value: Rc<T>)
pub fn set_rc_silent(&self, value: Rc<T>)
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.
sourceimpl<T: Default> Signal<T>
impl<T: Default> Signal<T>
sourcepub fn take(&self) -> Rc<T>
pub fn take(&self) -> Rc<T>
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.
sourcepub fn take_silent(&self) -> Rc<T>
pub fn take_silent(&self) -> Rc<T>
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>>
sourcepub fn get(&self) -> Rc<T>
pub fn get(&self) -> Rc<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);
sourcepub fn get_untracked(&self) -> Rc<T>
pub fn get_untracked(&self) -> Rc<T>
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);
sourcepub fn map<'a, U>(
&'a self,
cx: Scope<'a>,
f: impl FnMut(&T) -> U + 'a
) -> &'a ReadSignal<U>
pub fn map<'a, U>(
&'a self,
cx: Scope<'a>,
f: impl FnMut(&T) -> U + 'a
) -> &'a ReadSignal<U>
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);
sourcepub fn track(&self)
pub fn track(&self)
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
sourceimpl<T: AddAssign + Copy> AddAssign<T> for &Signal<T>
impl<T: AddAssign + Copy> AddAssign<T> for &Signal<T>
sourcefn add_assign(&mut self, other: T)
fn add_assign(&mut self, other: T)
Performs the +=
operation. Read more
sourceimpl<'a, T> AnyReadSignal<'a> for Signal<T>
impl<'a, T> AnyReadSignal<'a> for Signal<T>
sourcefn track(&self)
fn track(&self)
Call the ReadSignal::track
method.
sourceimpl<T> Deref for Signal<T>
impl<T> Deref for Signal<T>
type Target = ReadSignal<T>
type Target = ReadSignal<T>
The resulting type after dereferencing.
sourceimpl<T: DivAssign + Copy> DivAssign<T> for &Signal<T>
impl<T: DivAssign + Copy> DivAssign<T> for &Signal<T>
sourcefn div_assign(&mut self, other: T)
fn div_assign(&mut self, other: T)
Performs the /=
operation. Read more
sourceimpl<T: MulAssign + Copy> MulAssign<T> for &Signal<T>
impl<T: MulAssign + Copy> MulAssign<T> for &Signal<T>
sourcefn mul_assign(&mut self, other: T)
fn mul_assign(&mut self, other: T)
Performs the *=
operation. Read more
sourceimpl<T: SubAssign + Copy> SubAssign<T> for &Signal<T>
impl<T: SubAssign + Copy> SubAssign<T> for &Signal<T>
sourcefn sub_assign(&mut self, other: T)
fn sub_assign(&mut self, other: T)
Performs the -=
operation. Read more
impl<T: Eq> Eq for Signal<T>
Auto Trait Implementations
impl<T> !RefUnwindSafe for Signal<T>
impl<T> !Send for Signal<T>
impl<T> !Sync for Signal<T>
impl<T> Unpin for Signal<T>
impl<T> !UnwindSafe for Signal<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to key
and return true
if they are equal.