npm.io
1.0.1 • Published 6 years ago

@sevenoutman/use-abandon-on-unmount

Licence
MIT
Version
1.0.1
Deps
0
Size
3 kB
Vulns
0
Weekly
0

useAbandonOnUnmount

React Hook for abandoning promises upon component unmounting

yarn add @sevenoutman/use-abandon-on-unmount

Usage

import useAbandonOnUnmount from '@sevenoutman/use-abandon-on-unmount';

function App() {

    const [data, setData] = useState();

    const abandonOnUnmount = useAbandonOnUnmount();

    const fetchData = useCallback(async () => {
        const result = await abandonOnUnmount(requestData());
        setData(result); // Will not excute after this component unmounts
    }, [setData]);

    return (
       // ...
    );
}

Why?

Get you rid of this:

 Warning: Can only update a mounted or mounting component. This usually means you called setState, replaceState, or forceUpdate on an unmounted component. This is a no-op.

Keywords