yew_hooks/hooks/use_update.rs
1use std::rc::Rc;
2
3use yew::prelude::*;
4
5/// A hook returns a function that forces component to re-render when called.
6///
7/// # Example
8///
9/// ```rust
10/// # use yew::prelude::*;
11/// #
12/// use yew_hooks::prelude::*;
13///
14/// #[function_component(Update)]
15/// fn update() -> Html {
16/// let update = use_update();
17///
18/// let onclick = Callback::from(move |_| {
19/// update();
20/// });
21///
22/// html! {
23/// <>
24/// <button {onclick}>{ "Update" }</button>
25/// </>
26/// }
27/// }
28/// ```
29#[hook]
30pub fn use_update() -> Rc<dyn Fn()> {
31 let state = use_state(|| 0);
32
33 Rc::new(move || {
34 state.set((*state + 1) % 1_000_000);
35 })
36}