dioxus_hooks/
use_hook_did_run.rs

1use dioxus_core::{use_after_render, use_before_render, use_hook};
2use dioxus_signals::{CopyValue, WritableExt};
3
4/// A utility lifecycle hook that is intended to be used inside other hooks to determine if the outer hook has ran this render.
5/// The provided callback is executed after each render.
6/// The value will only be true if the containing outer hook is executed.
7#[doc = include_str!("../docs/rules_of_hooks.md")]
8#[doc = include_str!("../docs/moving_state_around.md")]
9pub fn use_hook_did_run(mut handler: impl FnMut(bool) + 'static) {
10    let mut did_run_ = use_hook(|| CopyValue::new(false));
11
12    // Before render always set the value to false
13    use_before_render(move || did_run_.set(false));
14
15    // Only when the outer hook is run do we want to set the value to true
16    did_run_.set(true);
17
18    // After render, we can check if the outer hook was run
19    use_after_render(move || handler(did_run_()));
20}