rep
repromised
npm i repromised
rep

repromised

🀝 Declarative promise resolver as a render props component

by Kohei

0.3.1 (see all)License:MITTypeScript:Built-In
npm i repromised
Readme

Repromised

npm GitHub stars GitHub license

Repromised is a component to build children by resolving a promise with Render Props pattern.

  • πŸš€ Dependency free
  • πŸ„β€ Extremely tiny
  • πŸ”Œ Plug and Play
  • πŸ‘· Well tested
  • πŸ‘” Built with TypeScript

Example

TRY IT OUT HERE

Repromised Example

Install

npm i -S repromised

APIs

<Repromised>

Props

NameTypeRequiredDescription
promise() => Promise<Value>βœ“A function returning a promise (it will be called when the component is mounted)
initialValue (the same type with the value what the promise should resolves)An initial value
children(snapshot: Snapshot) => ReactNodeA render props function

Snapshot

<Repromised> receives children as a render-props pattern function that gives a snapshot object which has the shape like the following:

NameTypeDescription
status"PENDING", "FULFILLED" or "REJECTED"The promise's status. In TypeScript you can use Status enum instead of string enum
valueValue (the value what the promise resolves) or nullThe value what the promise resolves. When status is "PENDING" or "REJECTED" and props.initial is not given, this is null
errorError or nullThe error when the promise rejects. This is null if status is not "REJECTED"
isLoadingbooleanA short hand value whether the promise's status is "PENDING" or not (same with to status === "PENDING").
requireValueValue (the value what the promise resolves)Same as value but it throws an error if value is null

Usage

import Repromised from 'repromised';

const SearchResult = ({ query }) => (
  <Repromised promise={() => fetchByQuery(query)} initial={[]} key={query}>
    {snapshot => snapshot.isLoading
      ? <Loading />
      : <ResultList>
          {snapshot.value.map(result => <ResultListItem result={result} />)}
        </ResultList>
    }
  </Repromised>
);

License

MIT

Contribute

Any feedback is welcome! You can help improving this project leaving Pull requests and helping with Issues.

Downloads/wk

11

GitHub Stars

20

LAST COMMIT

4yrs ago

MAINTAINERS

1

CONTRIBUTORS

0

OPEN ISSUES

0

OPEN PRs

0
VersionTagPublished
0.3.1
latest
4yrs ago
No alternatives found
No tutorials found
Add a tutorial

Rate & Review

100
No reviews found
Be the first to rate