Skip to content

React componentWillUnmount() analog? #340

Answered by scottbot95
INQTR asked this question in Q&A
Discussion options

You must be logged in to vote

It's kinda hacky and there probably should be a use_umount/use_drop, but you can implement Drop 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:

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),
    });
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by INQTR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants