Struct dioxus_hooks::UseState

source ·
pub struct UseState<T: 'static> { /* private fields */ }

Implementations§

source§

impl<T: 'static> UseState<T>

source

pub fn set(&self, new: T)

Set the state to a new value.

source

pub fn current(&self) -> Rc<T>

Get the current value of the state by cloning its container Rc.

This is useful when you are dealing with state in async contexts but need to know the current value. You are not given a reference to the state.

Examples

An async context might need to know the current value:

fn component(cx: Scope) -> Element {
    let count = use_state(cx, || 0);
    cx.spawn({
        let set_count = count.to_owned();
        async move {
            let current = set_count.current();
        }
    })
}
source

pub fn setter(&self) -> Rc<dyn Fn(T)>

Get the setter function directly without the UseState wrapper.

This is useful for passing the setter function to other components.

However, for most cases, calling to_owned on the state is the preferred way to get “another” state handle.

Examples

A component might require an Rc<dyn Fn(T)> as an input to set a value.

fn component(cx: Scope) -> Element {
    let value = use_state(cx, || 0);

    rsx!{
        Component {
            handler: value.setter()
        }
    }
}
source

pub fn modify(&self, f: impl FnOnce(&T) -> T)

Set the state to a new value, using the current state value as a reference.

This is similar to passing a closure to React’s set_value function.

Examples

Basic usage:

fn component(cx: Scope) -> Element {
    let value = use_state(cx, || 0);

    // to increment the value
    value.modify(|v| v + 1);

    // usage in async
    cx.spawn({
        let value = value.to_owned();
        async move {
            value.modify(|v| v + 1);
        }
    });

}
source

pub fn get(&self) -> &T

Get the value of the state when this handle was created.

This method is useful when you want an Rc around the data to cheaply pass it around your app.

Warning

This will return a stale value if used within async contexts.

Try current to get the real current value of the state.

Example
fn component(cx: Scope) -> Element {
    let value = use_state(cx, || 0);

    let as_rc = value.get();
    assert_eq!(as_rc.as_ref(), &0);

}
source

pub fn get_rc(&self) -> &Rc<T>

source

pub fn needs_update(&self)

Mark the component that create this UseState as dirty, forcing it to re-render.

fn component(cx: Scope) -> Element {
    let count = use_state(cx, || 0);
    cx.spawn({
        let count = count.to_owned();
        async move {
            // for the component to re-render
            count.needs_update();
        }
    })
}
source§

impl<T: Clone> UseState<T>

source

pub fn with_mut(&self, apply: impl FnOnce(&mut T))

Get a mutable handle to the value by calling ToOwned::to_owned on the current value.

This is essentially cloning the underlying value and then setting it, giving you a mutable handle in the process. This method is intended for types that are cheaply cloneable.

If you are comfortable dealing with RefMut, then you can use make_mut to get the underlying slot. However, be careful with RefMut since you might panic if the RefCell is left open.

Examples
let val = use_state(cx, || 0);

val.with_mut(|v| *v = 1);
source

pub fn make_mut(&self) -> RefMut<'_, T>

Get a mutable handle to the value by calling ToOwned::to_owned on the current value.

This is essentially cloning the underlying value and then setting it, giving you a mutable handle in the process. This method is intended for types that are cheaply cloneable.

Warning

Be careful with RefMut since you might panic if the RefCell is left open!

Examples
let val = use_state(cx, || 0);

*val.make_mut() += 1;
source

pub fn split(&self) -> (&T, &Self)

Convert this handle to a tuple of the value and the handle itself.

Trait Implementations§

source§

impl<T: Add + Copy> Add<T> for &UseState<T>

§

type Output = <T as Add<T>>::Output

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl<T: Add<Output = T> + Copy> AddAssign<T> for &UseState<T>

source§

fn add_assign(&mut self, rhs: T)

Performs the += operation. Read more
source§

impl<T: Add<Output = T> + Copy> AddAssign<T> for UseState<T>

source§

fn add_assign(&mut self, rhs: T)

Performs the += operation. Read more
source§

impl<T: Binary> Binary for UseState<T>

source§

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

Formats the value using the given formatter.
source§

impl<T: 'static> Clone for UseState<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> Debug for UseState<T>

source§

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

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

impl<T> Deref for UseState<T>

§

type Target = T

The resulting type after dereferencing.
source§

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

Dereferences the value.
source§

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

source§

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

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

impl<T: Div + Copy> Div<T> for &UseState<T>

§

type Output = <T as Div<T>>::Output

The resulting type after applying the / operator.
source§

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

Performs the / operation. Read more
source§

impl<T: Div<Output = T> + Copy> DivAssign<T> for &UseState<T>

source§

fn div_assign(&mut self, rhs: T)

Performs the /= operation. Read more
source§

impl<T: Div<Output = T> + Copy> DivAssign<T> for UseState<T>

source§

fn div_assign(&mut self, rhs: T)

Performs the /= operation. Read more
source§

impl<T: Mul + Copy> Mul<T> for &UseState<T>

§

type Output = <T as Mul<T>>::Output

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl<T: Mul<Output = T> + Copy> MulAssign<T> for &UseState<T>

source§

fn mul_assign(&mut self, rhs: T)

Performs the *= operation. Read more
source§

impl<T: Mul<Output = T> + Copy> MulAssign<T> for UseState<T>

source§

fn mul_assign(&mut self, rhs: T)

Performs the *= operation. Read more
source§

impl<T: Not + Copy> Not for &UseState<T>

§

type Output = <T as Not>::Output

The resulting type after applying the ! operator.
source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
source§

impl<T: Not + Copy> Not for UseState<T>

§

type Output = <T as Not>::Output

The resulting type after applying the ! operator.
source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
source§

impl<T: PartialEq> PartialEq<T> for UseState<T>

source§

fn eq(&self, other: &T) -> 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 PartialEq<bool> for &UseState<bool>

source§

fn eq(&self, other: &bool) -> 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> PartialEq<UseState<T>> for UseState<T>

source§

fn eq(&self, other: &UseState<T>) -> 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: PartialOrd> PartialOrd<T> for UseState<T>

source§

fn ge(&self, other: &T) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

fn gt(&self, other: &T) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
source§

fn le(&self, other: &T) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
source§

fn lt(&self, other: &T) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
source§

fn partial_cmp(&self, other: &T) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
source§

impl<T: PartialOrd> PartialOrd<UseState<T>> for UseState<T>

source§

fn ge(&self, other: &UseState<T>) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

fn gt(&self, other: &UseState<T>) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
source§

fn le(&self, other: &UseState<T>) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
source§

fn lt(&self, other: &UseState<T>) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
source§

fn partial_cmp(&self, other: &UseState<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
source§

impl<T: Sub + Copy> Sub<T> for &UseState<T>

§

type Output = <T as Sub<T>>::Output

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

impl<T: Sub<Output = T> + Copy> SubAssign<T> for &UseState<T>

source§

fn sub_assign(&mut self, rhs: T)

Performs the -= operation. Read more
source§

impl<T: Sub<Output = T> + Copy> SubAssign<T> for UseState<T>

source§

fn sub_assign(&mut self, rhs: T)

Performs the -= operation. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for UseState<T>

§

impl<T> !Send for UseState<T>

§

impl<T> !Sync for UseState<T>

§

impl<T> Unpin for UseState<T>

§

impl<T> !UnwindSafe for UseState<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,