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

Implementations

Set the state to a new value.

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();
        }
    })
}

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 oUseStatete is the preferred way to get “anothset_statetate 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()
        }
    }
}

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);
        }
    });

}

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

}

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();
        }
    })
}

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

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;

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

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

Performs the += operation. Read more

Performs the += operation. Read more

Formats the value using the given formatter.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

The resulting type after dereferencing.

Dereferences the value.

Formats the value using the given formatter. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

The resulting type after applying the ! operator.

Performs the unary ! operation. Read more

The resulting type after applying the ! operator.

Performs the unary ! operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

The resulting type after applying the - operator.

Performs the - operation. Read more

Performs the -= operation. Read more

Performs the -= operation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.