promise-hooks-react
    TypeScript icon, indicating that this package has built-in type declarations

    1.1.1 • Public • Published

    promise-hooks-react

    React Promise Hooks.

    Usage

    import { usePromise, useCallbackPromise } from 'promise-hooks-react';
    
    function ComponentA() {
      const [data, error, isLoading, reload] = usePromise(() => fetch(url), []);
      if (isLoading) {
        return 'Loading...';
      }
      return (
        <div>
          {error && <div className="error">Error: {error}</div>}
          {data && (
            <>
              <div className="data">Response: {data}</div>
              <button onClick={reload}>reload</button>
            </>
          )}
        </div>
      );
    }
    
    function ComponentB() {
      const [submit, data, error, isSubmitting] = useCallbackPromise((value) => fetch(url, { method: 'POST', body: value }), []);
    
      if (isLoading) {
        return 'Loading...';
      }
      return (
        <div>
          {error && <div className="error">Error: {error}</div>}
          {data && <div className="data">Response: {data}</div>}
          <button onClick={submit('some value')}>submit</button>
        </div>
      );
    }

    License

    MIT

    Install

    npm i promise-hooks-react

    DownloadsWeekly Downloads

    5

    Version

    1.1.1

    License

    MIT

    Unpacked Size

    17.6 kB

    Total Files

    27

    Last publish

    Collaborators

    • kamiaka