use_hook_did_run.rs 696 B

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