react-async-callback
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

react-async-callback

A small react helper snippet to handle async callbacks and turn their output into values hooked to the react life-cycle

⚠️ Relies on react-sync-promise internally.

Installation

npm install react-async-callback and there you go, nothing more needed

Usage

import React, { FC } from 'react';
import { isPending, isRejected, isResolved, ifUnresolved } from 'react-sync-promise';
import { useAsyncCallback } from 'react-async-callback';

export const PrequelsSurprise: FC = () => {
    const [result, onClick] = useAsyncCallback(() => Promise.resolve('Execute order 66'));

    return (
        <>
            <button type="button" aria-label="demo-button" onClick={onClick} />
            <p>{`Has not been called: ${result === null}`}</p>
            <p>{`Is loading: ${result !== null && isPending(result)}`}</p>
            <p>{`Was rejected: ${result !== null && isRejected(result)}`}</p>
            <p>{`Was finished: ${result !== null && isResolved(result)}`}</p>
            <p>{`Value or unresolved: ${result !== null && ifUnresolved(result, 'Pod racing')}`}</p>
        </>
    );
};

License

APACHE

Package Sidebar

Install

npm i react-async-callback

Weekly Downloads

2

Version

1.0.5

License

ISC

Unpacked Size

40.6 kB

Total Files

10

Last publish

Collaborators

  • filipomar