Struct intuitive::state::State

source ·
pub struct State<T> { /* private fields */ }
Expand description

A struct that triggers a re-render upon mutation.

State’s have interior mutability, and can be cloned. States cloned from one another share the inner reference to a T, and therefore mutating one of them will be reflected across all of the cloned states. For example,

let count = use_state(|| 0);

let other_count = count.clone();
other_count.set(1);

// both `count` and `other_count` are `1`
assert_eq!(count.get(), other_count.get());

This is useful when receiving a State as a parameter from a parent component, as it must be cloned, and then may be mutated by both the child and parent components.

Implementations§

source§

impl<T> State<T>

source

pub fn set(&self, new: T)

Sets a new value for the state and triggers a re-render.

source

pub fn inspect<F, R>(&self, f: F) -> Rwhere F: FnOnce(&T) -> R,

Calls a function on the inner value and returns its result. Does not trigger a re-render.

source

pub fn mutate<F, R>(&self, f: F)where F: FnOnce(&mut T) -> R,

Calls a function on a mutable reference of the inner value and triggers a re-render.

source

pub fn update<F>(&self, f: F)where F: FnOnce(&T) -> T,

Calls a function on the inner value, replaces it with the result, and triggers a re-render.

source§

impl<T: Clone> State<T>

source

pub fn get(&self) -> T

Returns a clone of the State<T>’s inner value.

Trait Implementations§

source§

impl<T> Clone for State<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: Default> Default for State<T>

source§

fn default() -> State<T>

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

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for State<T>

§

impl<T> Send for State<T>where T: Send,

§

impl<T> Sync for State<T>where T: Send,

§

impl<T> Unpin for State<T>

§

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