Function bounce::use_notion_applier

source ·
pub fn use_notion_applier<'hook, T>(
) -> impl 'hook + Hook<Output = Rc<dyn Fn(T)>>
where T: 'static + 'hook,
Expand description

A hook to create a function that applies a Notion.

A Notion is an action that can be dispatched to any state that accepts the dispatched notion.

Any type that is 'static can be dispatched as a notion.

Returns Rc<dyn Fn(T)>.

Note

When states receives a notion, it will be wrapped in an Rc<T>.

Example

pub struct Reset;

#[derive(PartialEq, Atom)]
#[bounce(with_notion(Reset))] // A #[bounce(with_notion(Notion))] needs to be denoted for the notion.
struct Username {
    inner: String,
}

// A WithNotion<T> is required for each notion denoted in the #[bounce(with_notion)] attribute.
impl WithNotion<Reset> for Username {
    fn apply(self: Rc<Self>, _notion: Rc<Reset>) -> Rc<Self> {
        Self::default().into()
    }
}

// second state
#[derive(PartialEq, Atom, Default)]
#[bounce(with_notion(Reset))]
struct Session {
    token: Option<String>,
}

impl WithNotion<Reset> for Session {
    fn apply(self: Rc<Self>, _notion: Rc<Reset>) -> Rc<Self> {
        Self::default().into()
    }
}
let reset_everything = use_notion_applier::<Reset>();
reset_everything(Reset);

Note

When used in function components and hooks, this hook is equivalent to:

pub fn use_notion_applier<T>() -> Rc<dyn Fn(T)>
where
    T: 'static,
{
    /* implementation omitted */
}