pub struct UseState<T>where
T: 'static,{ /* private fields */ }
Implementations§
§impl<T> UseState<T>where
T: 'static,
impl<T> UseState<T>where T: 'static,
pub fn set(&self, new: T)
pub fn set(&self, new: T)
Set the state to a new value.
pub fn current(&self) -> Rc<T, Global>
pub fn current(&self) -> Rc<T, Global>
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();
}
})
}
pub fn setter(&self) -> Rc<dyn Fn(T), Global>
pub fn setter(&self) -> Rc<dyn Fn(T), Global>
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()
}
}
}
pub fn modify(&self, f: impl FnOnce(&T) -> T)
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);
}
});
}
pub fn get(&self) -> &T
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);
}
pub fn get_rc(&self) -> &Rc<T, Global>
pub fn needs_update(&self)
pub fn needs_update(&self)
§impl<T> UseState<T>where
T: Clone,
impl<T> UseState<T>where T: Clone,
pub fn with_mut(&self, apply: impl FnOnce(&mut T))
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);
pub fn make_mut(&self) -> RefMut<'_, T>
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;
Trait Implementations§
§impl<T> AddAssign<T> for &UseState<T>where
T: Add<T, Output = T> + Copy,
impl<T> AddAssign<T> for &UseState<T>where T: Add<T, Output = T> + Copy,
§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
+=
operation. Read more§impl<T> AddAssign<T> for UseState<T>where
T: Add<T, Output = T> + Copy,
impl<T> AddAssign<T> for UseState<T>where T: Add<T, Output = T> + Copy,
§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
+=
operation. Read more§impl<T> DivAssign<T> for &UseState<T>where
T: Div<T, Output = T> + Copy,
impl<T> DivAssign<T> for &UseState<T>where T: Div<T, Output = T> + Copy,
§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
/=
operation. Read more§impl<T> DivAssign<T> for UseState<T>where
T: Div<T, Output = T> + Copy,
impl<T> DivAssign<T> for UseState<T>where T: Div<T, Output = T> + Copy,
§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
/=
operation. Read more§impl<T> MulAssign<T> for &UseState<T>where
T: Mul<T, Output = T> + Copy,
impl<T> MulAssign<T> for &UseState<T>where T: Mul<T, Output = T> + Copy,
§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
*=
operation. Read more§impl<T> MulAssign<T> for UseState<T>where
T: Mul<T, Output = T> + Copy,
impl<T> MulAssign<T> for UseState<T>where T: Mul<T, Output = T> + Copy,
§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
*=
operation. Read more§impl<T> PartialEq<UseState<T>> for UseState<T>
impl<T> PartialEq<UseState<T>> for UseState<T>
§impl<T> PartialOrd<T> for UseState<T>where
T: PartialOrd<T>,
impl<T> PartialOrd<T> for UseState<T>where T: PartialOrd<T>,
§fn ge(&self, other: &T) -> bool
fn ge(&self, other: &T) -> bool
self
and other
) and is used by the >=
operator. Read more§fn le(&self, other: &T) -> bool
fn le(&self, other: &T) -> bool
self
and other
) and is used by the <=
operator. Read more§impl<T> PartialOrd<UseState<T>> for UseState<T>where
T: PartialOrd<T>,
impl<T> PartialOrd<UseState<T>> for UseState<T>where T: PartialOrd<T>,
§fn ge(&self, other: &UseState<T>) -> bool
fn ge(&self, other: &UseState<T>) -> bool
self
and other
) and is used by the >=
operator. Read more§fn le(&self, other: &UseState<T>) -> bool
fn le(&self, other: &UseState<T>) -> bool
self
and other
) and is used by the <=
operator. Read moreAuto 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> 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
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere T: Any,
§fn into_any(self: Box<T, Global>) -> Box<dyn Any, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any, Global>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T, Global>) -> Rc<dyn Any, Global>
fn into_any_rc(self: Rc<T, Global>) -> Rc<dyn Any, Global>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.