0.3.1 • Published 5 years ago

repromised v0.3.1

Weekly downloads
8
License
MIT
Repository
github
Last release
5 years ago

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

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.

0.3.1

5 years ago

0.3.0

5 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago

0.0.0

6 years ago