Struct leptos::WriteSignal
source · pub struct WriteSignal<T>where
T: 'static,{ /* private fields */ }
Expand description
The setter for a reactive signal.
A signal is a piece of data that may change over time, and notifies other code when it has changed. This is the core primitive of Leptos’s reactive system.
Calling WriteSignal::update will mutate the signal’s value in place, and notify all subscribers that the signal’s value has changed.
WriteSignal
implements Fn, such that set_value(new_value)
is equivalent to
set_value.update(|value| *value = new_value)
.
WriteSignal
is Copy and 'static
, so it can very easily moved into closures
or copied structs.
Core Trait Implementations
.set()
(or calling the setter as a function) sets the signal’s value, and notifies all subscribers that the signal’s value has changed. to subscribe to the signal, and to re-run whenever the value of the signal changes..set_untracked()
sets the signal’s value without notifying its subscribers.
.update()
mutates the signal’s value in place and notifies all subscribers that the signal’s value has changed..update_untracked()
mutates the signal’s value in place without notifying its subscribers.
Examples
let (count, set_count) = create_signal(cx, 0);
// ✅ calling the setter sets the value
set_count(1);
assert_eq!(count(), 1);
// ❌ don't try to call the getter within the setter
// set_count(count() + 1);
// ✅ instead, use .update() to mutate the value in place
set_count.update(|count: &mut i32| *count += 1);
assert_eq!(count(), 2);
Trait Implementations§
source§impl<T> Clone for WriteSignal<T>
impl<T> Clone for WriteSignal<T>
source§fn clone(&self) -> WriteSignal<T>
fn clone(&self) -> WriteSignal<T>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl<T> Debug for WriteSignal<T>where
T: Debug + 'static,
impl<T> Debug for WriteSignal<T>where T: Debug + 'static,
source§impl<T> From<WriteSignal<T>> for SignalSetter<T>
impl<T> From<WriteSignal<T>> for SignalSetter<T>
source§fn from(value: WriteSignal<T>) -> SignalSetter<T>
fn from(value: WriteSignal<T>) -> SignalSetter<T>
Converts to this type from the input type.
source§impl<T> Hash for WriteSignal<T>where
T: Hash + 'static,
impl<T> Hash for WriteSignal<T>where T: Hash + 'static,
source§impl<T> PartialEq<WriteSignal<T>> for WriteSignal<T>where
T: PartialEq<T> + 'static,
impl<T> PartialEq<WriteSignal<T>> for WriteSignal<T>where T: PartialEq<T> + 'static,
source§fn eq(&self, other: &WriteSignal<T>) -> bool
fn eq(&self, other: &WriteSignal<T>) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.source§impl<T> SignalDispose for WriteSignal<T>
impl<T> SignalDispose for WriteSignal<T>
source§impl<T> SignalSet<T> for WriteSignal<T>
impl<T> SignalSet<T> for WriteSignal<T>
Examples
let (count, set_count) = create_signal(cx, 0);
// notifies subscribers
set_count.update(|n| *n = 1); // it's easier just to call set_count(1), though!
assert_eq!(count(), 1);
// you can include arbitrary logic in this update function
// also notifies subscribers, even though the value hasn't changed
set_count.update(|n| if *n > 3 { *n += 1 });
assert_eq!(count(), 1);
source§impl<T> SignalSetUntracked<T> for WriteSignal<T>where
T: 'static,
impl<T> SignalSetUntracked<T> for WriteSignal<T>where T: 'static,
source§fn set_untracked(&self, new_value: T)
fn set_untracked(&self, new_value: T)
Sets the signal’s value without notifying dependents.
source§fn try_set_untracked(&self, new_value: T) -> Option<T>
fn try_set_untracked(&self, new_value: T) -> Option<T>
Attempts to set the signal if it’s still valid. Returns
None
if the signal was set, [Some(T)
] otherwise.source§impl<T> SignalUpdate<T> for WriteSignal<T>
impl<T> SignalUpdate<T> for WriteSignal<T>
Examples
let (count, set_count) = create_signal(cx, 0);
// notifies subscribers
set_count.update(|n| *n = 1); // it's easier just to call set_count(1), though!
assert_eq!(count(), 1);
// you can include arbitrary logic in this update function
// also notifies subscribers, even though the value hasn't changed
set_count.update(|n| if *n > 3 { *n += 1 });
assert_eq!(count(), 1);
source§fn update(&self, f: impl FnOnce(&mut T))
fn update(&self, f: impl FnOnce(&mut T))
Applies a function to the current value to mutate it in place
and notifies subscribers that the signal has changed. Read more
source§impl<T> SignalUpdateUntracked<T> for WriteSignal<T>
impl<T> SignalUpdateUntracked<T> for WriteSignal<T>
source§fn update_untracked(&self, f: impl FnOnce(&mut T))
fn update_untracked(&self, f: impl FnOnce(&mut T))
Runs the provided closure with a mutable reference to the current
value without notifying dependents.
source§fn update_returning_untracked<U>(
&self,
f: impl FnOnce(&mut T) -> U
) -> Option<U>
fn update_returning_untracked<U>( &self, f: impl FnOnce(&mut T) -> U ) -> Option<U>
👎Deprecated: Please use
try_update_untracked
instead. This method will be removed in a future version of leptos
Runs the provided closure with a mutable reference to the current
value without notifying dependents and returns
the value the closure returned.
impl<T> Copy for WriteSignal<T>
impl<T> Eq for WriteSignal<T>where T: Eq + 'static,
impl<T> StructuralEq for WriteSignal<T>where T: 'static,
impl<T> StructuralPartialEq for WriteSignal<T>where T: 'static,
Auto Trait Implementations§
impl<T> RefUnwindSafe for WriteSignal<T>where T: RefUnwindSafe,
impl<T> Send for WriteSignal<T>where T: Send,
impl<T> Sync for WriteSignal<T>where T: Sync,
impl<T> Unpin for WriteSignal<T>where T: Unpin,
impl<T> UnwindSafe for WriteSignal<T>where T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CallHasher for Twhere
T: Hash + ?Sized,
impl<T> CallHasher for Twhere T: Hash + ?Sized,
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.