GitXplorerGitXplorer
r

use-cancelable-promise

public
10 stars
0 forks
13 issues

Commits

List of commits on branch master.
Unverified
1f7d644d237230d6b7aa9de43f6a250e1026c135

v0.0.2

rrodw1995 committed 5 years ago
Unverified
26154664f374471f89247ab38333fee6654dbae0

Allow type declaration

rrodw1995 committed 5 years ago
Unverified
1e2bd7adad29707efd76acb7d3b4a9c327ad486a

Initial files

rrodw1995 committed 5 years ago
Verified
7b193eadb5246952b030cbba5b585471ab86b6ac

Update README.md

rrodw1995 committed 5 years ago
Verified
ccbfe022c6b801d25f1b202833d3d4af69688c7e

Initial commit

rrodw1995 committed 5 years ago

README

The README file for this repository.

use-cancelable-promise

React hook to make promises cancelable and safe

Install

yarn add @rodw95/use-cancelable-promise

// or

npm install @rodw95/use-cancelable-promise

Example

import useCancelablePromise from '@rodw95/use-cancelable-promise';
import React, { useCallback, useState } from 'react';

export default () => {
  const makeCancelable = useCancelablePromise();
  const [state, setState] = useState(null);

  const doFetch = useCallback(
    () => makeCancelable(
      fetchAsync(),
      () => {
        // Optional: do someting on canceled, component is unmounted -> never set state!
        console.log('is canceled');
      },
    )
      .then((result) => {
        // Safe to set state because component is still mounted
        setState(result);
      }),
    [makeCancelable],
  );

  return (
    // ...
  );
};

License

MIT