Struct dioxus_signals::Signal
source · pub struct Signal<T: 'static> { /* private fields */ }
Expand description
Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.
use dioxus::prelude::*;
use dioxus_signals::*;
#[component]
fn App(cx: Scope) -> Element {
let mut count = use_signal(cx, || 0);
// Because signals have automatic dependency tracking, if you never read them in a component, that component will not be re-rended when the signal is updated.
// The app component will never be rerendered in this example.
render! { Child { state: count } }
}
#[component]
fn Child(cx: Scope, state: Signal<u32>) -> Element {
let state = *state;
use_future!(cx, |()| async move {
// Because the signal is a Copy type, we can use it in an async block without cloning it.
*state.write() += 1;
});
render! {
button {
onclick: move |_| *state.write() += 1,
"{state}"
}
}
}
Implementations§
source§impl<T: 'static> Signal<Vec<T>>
impl<T: 'static> Signal<Vec<T>>
source§impl<T: 'static> Signal<Option<T>>
impl<T: 'static> Signal<Option<T>>
sourcepub fn get_or_insert(&self, default: T) -> Ref<'_, T>
pub fn get_or_insert(&self, default: T) -> Ref<'_, T>
Gets the value out of the Option, or inserts the given value if the Option is empty.
sourcepub fn get_or_insert_with(&self, default: impl FnOnce() -> T) -> Ref<'_, T>
pub fn get_or_insert_with(&self, default: impl FnOnce() -> T) -> Ref<'_, T>
Gets the value out of the Option, or inserts the value returned by the given function if the Option is empty.
source§impl<T: 'static> Signal<T>
impl<T: 'static> Signal<T>
sourcepub fn new(value: T) -> Self
pub fn new(value: T) -> Self
Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.
sourcepub fn new_in_scope(value: T, owner: ScopeId) -> Self
pub fn new_in_scope(value: T, owner: ScopeId) -> Self
Create a new signal with a custom owner scope. The signal will be dropped when the owner scope is dropped instead of the current scope.
sourcepub fn origin_scope(&self) -> ScopeId
pub fn origin_scope(&self) -> ScopeId
Get the scope the signal was created in.
sourcepub fn read(&self) -> Ref<'_, T>
pub fn read(&self) -> Ref<'_, T>
Get the current value of the signal. This will subscribe the current scope to the signal. If the signal has been dropped, this will panic.
sourcepub fn write(&self) -> Write<'_, T>
pub fn write(&self) -> Write<'_, T>
Get a mutable reference to the signal’s value. If the signal has been dropped, this will panic.
sourcepub fn set(&self, value: T)
pub fn set(&self, value: T)
Set the value of the signal. This will trigger an update on all subscribers.
Trait Implementations§
source§impl<T: Add<Output = T> + Copy + 'static> AddAssign<T> for Signal<T>
impl<T: Add<Output = T> + Copy + 'static> AddAssign<T> for Signal<T>
source§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
+=
operation. Read moresource§impl<T: Div<Output = T> + Copy + 'static> DivAssign<T> for Signal<T>
impl<T: Div<Output = T> + Copy + 'static> DivAssign<T> for Signal<T>
source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
/=
operation. Read moresource§impl<T: Mul<Output = T> + Copy + 'static> MulAssign<T> for Signal<T>
impl<T: Mul<Output = T> + Copy + 'static> MulAssign<T> for Signal<T>
source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
*=
operation. Read moresource§impl<T: 'static> PartialEq<Signal<T>> for Signal<T>
impl<T: 'static> PartialEq<Signal<T>> for Signal<T>
source§impl<T: Sub<Output = T> + Copy + 'static> SubAssign<T> for Signal<T>
impl<T: Sub<Output = T> + Copy + 'static> SubAssign<T> for Signal<T>
source§fn sub_assign(&mut self, rhs: T)
fn sub_assign(&mut self, rhs: T)
-=
operation. Read more