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>>

source

pub fn get(&self, index: usize) -> Option<Ref<'_, T>>

Read a value from the inner vector.

source§

impl<T: 'static> Signal<Option<T>>

source

pub fn unwrap(&self) -> Twhere T: Clone,

Unwraps the inner value and clones it.

source

pub fn as_ref(&self) -> Option<Ref<'_, T>>

Attemps to read the inner value of the Option.

source§

impl<T: 'static> Signal<Vec<T>>

source

pub fn push(&self, value: T)

Pushes a new value to the end of the vector.

source

pub fn pop(&self) -> Option<T>

Pops the last value from the vector.

source

pub fn insert(&self, index: usize, value: T)

Inserts a new value at the given index.

source

pub fn remove(&self, index: usize) -> T

Removes the value at the given index.

source

pub fn clear(&self)

Clears the vector, removing all values.

source

pub fn extend(&self, iter: impl IntoIterator<Item = T>)

Extends the vector with the given iterator.

source

pub fn truncate(&self, len: usize)

Truncates the vector to the given length.

source

pub fn swap_remove(&self, index: usize) -> T

Swaps two values in the vector.

source

pub fn retain(&self, f: impl FnMut(&T) -> bool)

Retains only the values that match the given predicate.

source

pub fn split_off(&self, at: usize) -> Vec<T>

Splits the vector into two at the given index.

source§

impl<T: 'static> Signal<Option<T>>

source

pub fn take(&self) -> Option<T>

Takes the value out of the Option.

source

pub fn replace(&self, value: T) -> Option<T>

Replace the value in the Option.

source

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.

source

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<Vec<T>>

source

pub fn get_mut(&self, index: usize) -> Option<Write<'_, T, Vec<T>>>

Returns a reference to an element or None if out of bounds.

source§

impl<T: 'static> Signal<Option<T>>

source

pub fn as_mut(&self) -> Option<Write<'_, T, Option<T>>>

Returns a reference to an element or None if out of bounds.

source§

impl<T: 'static> Signal<T>

source

pub fn new(value: T) -> Self

Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.

source

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.

source

pub fn origin_scope(&self) -> ScopeId

Get the scope the signal was created in.

source

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.

source

pub fn write(&self) -> Write<'_, T>

Get a mutable reference to the signal’s value. If the signal has been dropped, this will panic.

source

pub fn set(&self, value: T)

Set the value of the signal. This will trigger an update on all subscribers.

source

pub fn with<O>(&self, f: impl FnOnce(&T) -> O) -> O

Run a closure with a reference to the signal’s value. If the signal has been dropped, this will panic.

source

pub fn with_mut<O>(&self, f: impl FnOnce(&mut T) -> O) -> O

Run a closure with a mutable reference to the signal’s value. If the signal has been dropped, this will panic.

source§

impl<T: Clone + 'static> Signal<T>

source

pub fn value(&self) -> 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.

source§

impl Signal<bool>

source

pub fn toggle(&self)

Invert the boolean value of the signal. This will trigger an update on all subscribers.

Trait Implementations§

source§

impl<T: Add<Output = T> + Copy + 'static> Add<T> for Signal<T>

§

type Output = T

The resulting type after applying the + operator.
source§

fn add(self, rhs: T) -> Self::Output

Performs the + operation. Read more
source§

impl<T: Add<Output = T> + Copy + 'static> AddAssign<T> for Signal<T>

source§

fn add_assign(&mut self, rhs: T)

Performs the += operation. Read more
source§

impl<T> Clone for Signal<T>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug + 'static> Debug for Signal<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Default + 'static> Default for Signal<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T> Deref for Signal<T>

§

type Target = dyn Fn() -> Ref<'static, T>

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T: Display + 'static> Display for Signal<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Div<Output = T> + Copy + 'static> Div<T> for Signal<T>

§

type Output = T

The resulting type after applying the / operator.
source§

fn div(self, rhs: T) -> Self::Output

Performs the / operation. Read more
source§

impl<T: Div<Output = T> + Copy + 'static> DivAssign<T> for Signal<T>

source§

fn div_assign(&mut self, rhs: T)

Performs the /= operation. Read more
source§

impl<T: Clone + 'static> IntoIterator for Signal<Vec<T>>

§

type IntoIter = SignalIterator<T>

Which kind of iterator are we turning this into?
§

type Item = T

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T: Mul<Output = T> + Copy + 'static> Mul<T> for Signal<T>

§

type Output = T

The resulting type after applying the * operator.
source§

fn mul(self, rhs: T) -> Self::Output

Performs the * operation. Read more
source§

impl<T: Mul<Output = T> + Copy + 'static> MulAssign<T> for Signal<T>

source§

fn mul_assign(&mut self, rhs: T)

Performs the *= operation. Read more
source§

impl<T: 'static> PartialEq<Signal<T>> for Signal<T>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Sub<Output = T> + Copy + 'static> Sub<T> for Signal<T>

§

type Output = T

The resulting type after applying the - operator.
source§

fn sub(self, rhs: T) -> Self::Output

Performs the - operation. Read more
source§

impl<T: Sub<Output = T> + Copy + 'static> SubAssign<T> for Signal<T>

source§

fn sub_assign(&mut self, rhs: T)

Performs the -= operation. Read more
source§

impl<T> Copy 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>where T: Unpin,

§

impl<T> !UnwindSafe for Signal<T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> Dep for Twhere T: 'static + PartialEq<T> + Clone,