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(0);
// ✅ calling the setter sets the value
// `set_count(1)` on nightly
set_count.set(1);
assert_eq!(count.get(), 1);
// ❌ you could call the getter within the setter
// set_count.set(count.get() + 1);
// ✅ however it's more efficient to use .update() and mutate the value in place
set_count.update(|count: &mut i32| *count += 1);
assert_eq!(count.get(), 2);
Trait Implementations§
Source§impl<T> Clone for WriteSignal<T>
impl<T> Clone for WriteSignal<T>
Source§impl<T> Debug for WriteSignal<T>
impl<T> Debug for WriteSignal<T>
Source§impl<T> From<WriteSignal<T>> for SignalSetter<T>
impl<T> From<WriteSignal<T>> for SignalSetter<T>
Source§fn from(value: WriteSignal<T>) -> Self
fn from(value: WriteSignal<T>) -> Self
Converts to this type from the input type.
Source§impl<T> Hash for WriteSignal<T>
impl<T> Hash for WriteSignal<T>
Source§impl<T> PartialEq for WriteSignal<T>
impl<T> PartialEq for WriteSignal<T>
Source§impl<T> SignalDispose for WriteSignal<T>
impl<T> SignalDispose for WriteSignal<T>
Source§impl<T> SignalSet for WriteSignal<T>
§Examples
let (count, set_count) = create_signal(0);
// notifies subscribers
set_count.update(|n| *n = 1); // it's easier just to call set_count.set(1), though!
assert_eq!(count.get(), 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.get(), 1);
impl<T> SignalSet for WriteSignal<T>
§Examples
let (count, set_count) = create_signal(0);
// notifies subscribers
set_count.update(|n| *n = 1); // it's easier just to call set_count.set(1), though!
assert_eq!(count.get(), 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.get(), 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 for WriteSignal<T>
§Examples
let (count, set_count) = create_signal(0);
// notifies subscribers
set_count.update(|n| *n = 1); // it's easier just to call set_count.set(1), though!
assert_eq!(count.get(), 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.get(), 1);
impl<T> SignalUpdate for WriteSignal<T>
§Examples
let (count, set_count) = create_signal(0);
// notifies subscribers
set_count.update(|n| *n = 1); // it's easier just to call set_count.set(1), though!
assert_eq!(count.get(), 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.get(), 1);
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.
impl<T> Copy for WriteSignal<T>
impl<T> Eq for WriteSignal<T>
Auto Trait Implementations§
impl<T> Freeze for WriteSignal<T>
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.