React componentWillUnmount() analog? #340
-
Maybe a stupid question) How to perform some logic when component will be unmounted? |
Beta Was this translation helpful? Give feedback.
Answered by
scottbot95
Oct 21, 2022
Replies: 1 comment
-
It's kinda hacky and there probably should be a I actually ended up writing my own hook to do this in a somewhat convenient way: pub struct UseDrop {
callback: Box<dyn Fn()>,
}
impl Drop for UseDrop {
fn drop(&mut self) {
(self.callback)();
}
}
pub fn use_drop(cx: &ScopeState, callback: impl Fn() + 'static) {
cx.use_hook(|_| UseDrop {
callback: Box::new(callback),
});
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
INQTR
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's kinda hacky and there probably should be a
use_umount
/use_drop
, but you can implementDrop
on the value you send to ScopeState.use_hook to get a callback when the component is dropped.I actually ended up writing my own hook to do this in a somewhat convenient way: