Code Monkey home page Code Monkey logo

repromised's Introduction

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

Name Type Required Description
promise () => Promise<Value> โœ“ A function returning a promise (it will be called when the component is mounted)
initial Value (the same type with the value what the promise should resolves) An initial value
children (snapshot: Snapshot) => ReactNode A 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:

Name Type Description
status "PENDING", "FULFILLED" or "REJECTED" The promise's status. In TypeScript you can use Status enum instead of string enum
value Value (the value what the promise resolves) or null The value what the promise resolves. When status is "PENDING" or "REJECTED" and props.initial is not given, this is null
error Error or null The error when the promise rejects. This is null if status is not "REJECTED"
isLoading boolean A short hand value whether the promise's status is "PENDING" or not (same with to status === "PENDING").
requireValue Value (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.

repromised's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

spiritualism

repromised's Issues

Allow initial to be nullable

Allow initial prop to be type Value | null | undefined. Especially for promises that can return an object, that object may Required<Value> so having to define each item is cumbersome.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.