npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-promise-sway-fork

v2.0.3

Published

a react.js component for declarative way of handling promises

Downloads

10

Readme

react-promise

NPM badge

a react.js component for general promise - no need for stateful component just to render a value hidden behind a promise or for a simple form. Let's consider a trivial example: you have a promise such as this

let prom = new Promise(function(resolve, reject) {
  setTimeout(function() {
    resolve('a value')
  }, 100)
})

and you want to make a component, which renders out in it's body 'a value'. Without react-promise, such component looks like this:

class ExampleWithoutAsync extends React.Component { // you can't use stateless component because you need a state
  constructor () {
    super()
    this.state = {}
    prom.then((value) => {
      this.setState({val: value})
    })
  }
  render () {
    if (!this.state.val) return
    return <div>{this.state.val}</div>
  }

and with react-promise:

import Async from 'react-promise'

const ExampleWithAsync = props => (
  <Async promise={prom} then={val => <div>{val}</div>} />
)

Much simpler, right? Because the latter code handles that promise declaratively instead of imperatively. In case you need user input before you can make the async call, there is a before property. Assign a function into it if you need to render a form for example.

<Async
  before={handlePromise => {
    return (
      <form>
        <input />
        <button
          onClick={() => {
            handlePromise(Promise.resolve('awesome data'))
          }}
        >
          do something async like a POST request
        </button>
      </form>
    )
  }}
/>

The form is rendered before the promise is resolved. If you ever need to reset the Async to before after promise has resolved/rejected get the Async ref and use

ref.setState({ status: 'none' })

install

With npm:

npm i react-promise

defaultPending

You can define a single pending state for all instances of <Async /> by defining a defaultPending property on the Async component class. Full example here:

import Async from 'react-promise'

Async.defaultPending = (
  <h1>my uber loading spinner/text/whatever used for all</h1>
)

Available props:

All props are optional

  • promise a promise you want to wait for
  • before if no promise is provided, Async will invoke this inside it's render method-use for forms and such
  • then runs when promise is resolved. Async will run function provided in it's render passing a resolved value as first parameter.
  • catch runs when promise is rejected. Async will run function provided in it's render passing an error as first parameter.
  • pending can be a React node which will be outputted from Async render method while promise is pending. Can be a function too. If none is provided and defaultPending is set, then outputs the default.

To use with Typescript

import Async, { Props as AsyncProps } from 'react-promise'

const StringAsync = Async as { new (props: AsyncProps<string>): Async<string> }

The type used for the generic will be matched against the type for the promise's value. This workaround is necessary because currently there's no way to directly supply generic types in Typescript.