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 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-inject-promise

v0.0.7

Published

Inject a promised value into a react component, either by 'higher order component' or by 'function as children'.

Readme

travis ci

React Inject Promise

Hocs which resolve and inject promises.

  • Used with recompose is recommended.

Install

npm install react-inject-promise or yarn add react-inject-promise

injectPromise

type ReactComponent<Props> = React.StatelessComponent<Props>|React.ComponentClass<Props>;

// A hoc(higher-order-component)
injectPromise<Props>(arg:{
    values:{
        [name:string]:(props:Props)=>Promise<any>
    },
    shouldReload:(p1:Props,p2:Props)=>boolean
})(Component:ReactComponent<Props>)

Inject the promise(s), the injected props are:

  • [name]
    • the user-defined name of that promise.
  • [name]Loading
    • Whether the promise status is "resolving"
  • reload[Name]
    • [Name] is capitalized [name]
    • Reload the promise, which could means re-fetch data from remote api.
  • set[Name]
    • Manully update the resolved value

RenderPromise

Render a function as children, the argument passed to that function is similar to 'injectPromise':

  • value
  • valueLoading
  • reloadValue
  • setValue

How to use

Usage 1

import * as React from "react"
import {injectPromise} from "react-inject-promise"

@injectPromise({
    values:{
        users:(props)=>fetch(`/users?group=${props.group}`).then(r=>r.json()),
        //todos: ...
    },
    shouldReload=(props,nextProps)=>props.group!==nextProps.group
})
class Haha extends React.PureComponent<any,any>{
    render(){
        const {
            users, // undefined or the resolved value
            reloadUsers, // a callback to re-resolve the promise
            setUsers, // set the resolved value manually
            usersLoading // whether the promise is not resolved yet.
        } = this.props
        if(usersLoading)
            return <div>loading...</div>
        return <div>
            {
                JSON.stringify(users)
            }
            <button onClick={reloadUsers}>Reload Users</button>
            <button onClick={()=>{
                fetch(`addUser`,{
                    body:JSON.stringify({name:'Shane'})
                }).then((newUser)=>{
                    setUsers(users.concat(newUser))
                    //or reloadUser()
                })
            }}>Add User</button>
        </div>
    }
}

Usage 2:

import * as React from "react"
import {RenderPromise} from "react-inject-promise"

const fetchUsers = (groupId)=>fetch(`/users?group=${groupId}`).then(r=>r.json())

function Component(props){
    /*
     * when reloadFlag changes, the 'promise' prop will be re-called, e.g. reload data from remote API.
     * it is ok to pass an inline function as 'promise' props, because RenderPromise will only use the first 'promise' prop it sees, except when reloadFlag has changed.
     */
    return <RenderPromise promise={()=>fetchUsers(props.groupId)} reloadFlag={props.groupId}>
        {({
            value,
            valueLoading,
            setValue,
            reloadValue
        })=>{
            return valueLoading?"loading...":<p>{value}</p>
        }}
    </RenderPromise>
}