3.3.0 ā€¢ Published 3 years ago

@jameslnewell/react-promise v3.3.0

Weekly downloads
73
License
MIT
Repository
github
Last release
3 years ago

@jameslnewell/react-promise

npm (scoped) Bundle Size Actions Status

šŸŽ£ React hooks and resources for working with promises.

If you need to work with observables, try @jameslnewell/react-observable.

Installation

NPM:

npm install @jameslnewell/react-promise

Yarn:

yarn add @jameslnewell/react-promise

Usage

You'll find a working example of react-promise in CodeSandbox.

usePromise

Start resolving a promise as soon as the component mounts.

Parameters:

  • keys - A unique set of keys for the promise.
  • factory - A function which creates the promise.
  • options - Options to configure the behaviour of the hook.

Returns:

  • .status - Whether the promise is pending, fulfilled or rejected.
  • .value - The value of the promise after it is fulfilled.
  • .error - The value of the promise after it is rejected.
  • .isPending - Whether we're waiting for the promise to be fulfilled or rejected.
  • .isFulfilled - Whether the promise has been fulfilled.
  • .isRejected- Whether the promise has rejected.
  • .invoke- A function to invoke the promise again.
import React from 'react';
import {usePromise} from '@jameslnewell/react-promise';

const getUser = async (id) => {
  const res = await fetch(`/user/${id}`, {method: 'GET'});
  const data = await res.json();
  return data;
};

const UserProfile = ({id}) => {
  const {value} = usePromise([id], () => getUser(id));
  switch (true) {
    case error:
      return <p>Sorry, something went wrong.</p>;
    case value:
      return (
        <p>
          Hello <strong>{user.name}</strong>!
        </p>
      );
    default:
      return <p>Loading...</p>;
  }
};

useDeferredPromise

Start resolving a promise when invoked manually.

Parameters:

  • keys - A unique set of keys for the promise.
  • factory - A function which creates the promise.
  • options - Options to configure the behaviour of the hook.

Returns:

  • .status - Whether the promise is pending, fulfilled or rejected.
  • .value - The value of the promise after it is fulfilled.
  • .error - The value of the promise after it is rejected.
  • .isPending - Whether we're waiting for the promise to be fulfilled or rejected.
  • .isFulfilled - Whether the promise has been fulfilled.
  • .isRejected- Whether the promise has rejected.
  • .invoke- A function to invoke the promise again.
import React, {useState, useEffect} from 'react';
import {useDeferredPromise} from '@jameslnewell/react-promise';

const putUser = async (id, data) => {
  await fetch(`/user/${id}`, {
    method: 'POST',
    body: JSON.stringify(data),
  });
};

const EditUserProfile = ({id}) => {
  const input = React.useRef(null);
  const {isPending, invoke: save} = useDeferredPromise([id], (data) =>
    putUser(id, data),
  );
  const handleSubmit = async (event) => {
    event.preventDefault();
    save({name: input.current.value});
  };
  return (
    <form onSubmit={handleSubmit}>
      <input ref={input} />
      <button disabled={isPending}>Save</button>
    </form>
  );
};
5.0.0-preview.3

3 years ago

5.0.0-preview.2

3 years ago

5.0.0-preview.1

3 years ago

4.0.0-preview.11

3 years ago

4.0.0-preview.10

3 years ago

4.0.0-preview.9

3 years ago

4.0.0-preview.8

3 years ago

4.0.0-preview.7

3 years ago

4.0.0-preview.6

3 years ago

4.0.0-preview.5

3 years ago

4.0.0-preview.4

3 years ago

4.0.0-preview.3

3 years ago

4.0.0-preview.2

3 years ago

4.0.0-preview.1

3 years ago

3.3.0

4 years ago

3.2.1

4 years ago

3.2.0

5 years ago

3.1.0

5 years ago

3.0.6

5 years ago

3.0.4

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

1.0.0-preview.2

5 years ago

1.0.0-preview.1

5 years ago