Struct fermi::hooks::AtomState

source ·
pub struct AtomState<V: 'static> { /* private fields */ }

Implementations§

source§

impl<T: 'static> AtomState<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 AtomState 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 all consumers of this atom 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> AtomState<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 &AtomState<T>

§

type Output = <T as Add>::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 &AtomState<T>

source§

fn add_assign(&mut self, rhs: T)

Performs the += operation. Read more
source§

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

source§

fn add_assign(&mut self, rhs: T)

Performs the += operation. Read more
source§

impl<T: Binary> Binary for AtomState<T>

source§

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

Formats the value using the given formatter.
source§

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

source§

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

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

impl<T> Deref for AtomState<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 AtomState<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 &AtomState<T>

§

type Output = <T as Div>::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 &AtomState<T>

source§

fn div_assign(&mut self, rhs: T)

Performs the /= operation. Read more
source§

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

source§

fn div_assign(&mut self, rhs: T)

Performs the /= operation. Read more
source§

impl<V> Drop for AtomState<V>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

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

§

type Output = <T as Mul>::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 &AtomState<T>

source§

fn mul_assign(&mut self, rhs: T)

Performs the *= operation. Read more
source§

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

source§

fn mul_assign(&mut self, rhs: T)

Performs the *= operation. Read more
source§

impl<T: Not + Copy> Not for &AtomState<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 AtomState<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 AtomState<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 &AtomState<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> PartialEq for AtomState<T>

source§

fn eq(&self, other: &AtomState<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: Sub + Copy> Sub<T> for &AtomState<T>

§

type Output = <T as Sub>::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 &AtomState<T>

source§

fn sub_assign(&mut self, rhs: T)

Performs the -= operation. Read more
source§

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

source§

fn sub_assign(&mut self, rhs: T)

Performs the -= operation. Read more

Auto Trait Implementations§

§

impl<V> !RefUnwindSafe for AtomState<V>

§

impl<V> !Send for AtomState<V>

§

impl<V> !Sync for AtomState<V>

§

impl<V> Unpin for AtomState<V>

§

impl<V> !UnwindSafe for AtomState<V>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where 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 T
where 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 T
where 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 T
where 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