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-async-await

v1.5.0

Published

Async component that waits on promises to resolve!

Downloads

91

Readme

react-async-await

Async component that waits on promises to resolve!

Motivation

Creating a promise is a synchronous action. If you hold a reference to a promise, you can eventually get the future value that it resolves to. Calling setState asynchronously in a component can cause a lot of headaches because of race conditions. The promise could still be in a pending state when the component unmounts or the props change. The Async component allows you to never worry about these race conditions and enables you to write your asynchronous react code as if it was synchronous.

Install

yarn add react-async-await react

ReactAsyncAwait

ReactAsyncAwait~Async ⇒ ReactElement

Component that takes a promise and injects the render callback with the resolved value.

Kind: inner property of ReactAsyncAwait
Extends: ReactComponent

| Param | Type | Description | | ---------------- | --------------------- | ----------------------------------------- | | props | object | | | [props.await] | * | | | [props.waiting] | function | map promise to value | | [props.then] | function | map result to value | | [props.catch] | function | map error to value (default throws error) | | [props.children] | function | render callback |

Example

import React from "react";
import ReactDOM from "react-dom";
import { Async } from "react-async-await";

class LoadUser extends React.Component {
  componentWillMount() {
    this.setState({
      error: undefined,
      promise: undefined
    });
  }

  componentDidMount() {
    this.setState({
      promise: fetch(`/api/users/${this.props.id}`).then(r => r.json())
    });
  }

  componentWillReceiveProps(nextProps) {
    if (this.props.id !== nextProps.id) {
      this.setState({
        error: undefined,
        promise: fetch(`/api/users/${nextProps.id}`).then(r => r.json())
      });
    }
  }

  componentDidCatch(error) {
    this.setState({ error });
  }

  render() {
    return this.state.error ? (
      <div>Uncaught promise rejection: {this.state.error.message}</div>
    ) : (
      <Async await={this.state.promise}>{this.props.children}</Async>
    );
  }
}

ReactDOM.render(
  <LoadUser id={1}>
    {user =>
      typeof user === undefined ? (
        <div>Loading...</div>
      ) : (
        <h1>Hello {user.name}!</h1>
      )
    }
  </LoadUser>,
  document.getElementById("root")
);

ReactAsyncAwait~createLoader(loader, [resolver]) ⇒ ReactComponent

Create a wrapper component around Async that maps props to a promise when the component mounts.

Kind: inner method of ReactAsyncAwait

| Param | Type | Description | | ---------- | --------------------- | ------------------------------ | | loader | function | loader maps props to a promise | | [resolver] | function | resolver maps props to a key |

Example

import React from "react";
import ReactDOM from "react-dom";
import { createLoader } from "react-async-await";

const LoadUser = createLoader(
  props => fetch(`/api/users/${props.id}`).then(r => r.json()),
  props => props.id // when key changes the loader is called again
);

ReactDOM.render(
  <LoadUser id={1}>
    {user =>
      typeof user === undefined ? (
        <div>Loading...</div>
      ) : (
        <h1>Hello {user.name}!</h1>
      )
    }
  </LoadUser>,
  document.getElementById("root")
);

Example (memoized loader)

import React from "react";
import ReactDOM from "react-dom";
import { createLoader } from "react-async-await";
// https://lodash.com/docs/4.17.5#memoize
import memoize from "lodash/memoize";

const loader = memoize(
  props => fetch(`/api/users/${props.id}`).then(r => r.json()),
  props => props.id // key is used to resolve to cached value
);

const LoadUser = createLoader(
  loader,
  props => props.id // when key changes the loader is called again
);

ReactDOM.render(
  <LoadUser id={1}>
    {user =>
      typeof user === undefined ? (
        <div>Loading...</div>
      ) : (
        <h1>Hello {user.name}!</h1>
      )
    }
  </LoadUser>,
  document.getElementById("root")
);