dioxus_hooks/use_hook_did_run.rs
1use dioxus_core::prelude::*;
2use dioxus_signals::{CopyValue, Writable};
3
4/// A hook that uses before/after lifecycle hooks to determine if the hook was run
5#[doc = include_str!("../docs/rules_of_hooks.md")]
6#[doc = include_str!("../docs/moving_state_around.md")]
7pub fn use_hook_did_run(mut handler: impl FnMut(bool) + 'static) {
8 let mut did_run_ = use_hook(|| CopyValue::new(false));
9
10 // Before render always set the value to false
11 use_before_render(move || did_run_.set(false));
12
13 // Only when this hook is hit do we want to set the value to true
14 did_run_.set(true);
15
16 // After render, we can check if the hook was run
17 use_after_render(move || handler(did_run_()));
18}