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. State
s 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>
impl<T> State<T>
sourcepub fn inspect<F, R>(&self, f: F) -> Rwhere
F: FnOnce(&T) -> R,
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.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more