Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useEffectAsync doesn't support cleanup function. #1

Open
platoonic opened this issue Jan 16, 2021 · 2 comments
Open

useEffectAsync doesn't support cleanup function. #1

platoonic opened this issue Jan 16, 2021 · 2 comments

Comments

@platoonic
Copy link

Hey.
Nice work with the library! Though useEffect is supposed to return a function as an equivalent to componentDidUnmount, this doesn't seem to be the case with useEffectAsync since the API you've provided doesn't allow passing a function for cleanup.

@ihabshea
Copy link
Owner

Hello,
Have you tried adding a return function with your cleanup operations like this:
return ( () => { unsubscribe(); });

@platoonic
Copy link
Author

I haven't tried it yet tbh but looking how useEffectAsync is implemented, it doesn't seem to be handling the unmounting phase at all.

export default function useEffectAsync(effect, inputs) {
    useEffect(() => {
        effect();
    }, inputs);
}

Thing is, adding a line like this:

return effect();

Will trigger the same problem of useEffect not supporting a non-functional return type since it's returning a promise object. I think there should be some sort of API that handles passing those cleanup functions, one that's different from how the original useEffect handles it.
This package for example takes cleanup function as a second optional argument in its useAsyncEffect hook:
https://github.com/rauldeheer/use-async-effect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants