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-wait2

v0.1.0

Published

Show placeholder when waiting, show content when done

Downloads

1

Readme

react-wait2

Show placeholder when waiting, show your content when done, show error when failed.

Motivation

There's already a package called react-placeholder, why bother making a similar one? the reasons are:

  • decouple the content loader, I suggest using react-content-loader as the content loader
  • make content loader more customizable, using the technique render props
  • make loading data the responsibility of children element, let the tasks like show spinner or error warning be the responsibility of react-wait

Installation

$ npm i react-wait

Usage

import { Wait, STATUS } from 'react-wait'

An example

import react from React
import { Wait } from 'react-wait'
import { Facebook } from 'react-content-loader'

class UserInfoWithLoader extends React.Component {

  render () {
    let { userInfo } = this.state
    return <Wait renderPlaceholder={() => <Facebook />}>
      ({ start, succeed, fail } => {
        <UserInfo dataLoaded={succeed} />
      })
  	</Wait>
  }
}

class UserInfo extends React.Component {

  state = {
  	userInfo: null
  }

  async componentDidMount () {
    this.setState({ userInfo: await loadUserInfo() })
    this.props.dataLoaded(userInfo)
  }

  render () {
    let { userInfo } = this.state
    return userInfo ? null : <div>{userInfo.name}</div>
  }
}

check test.js and example for more details

API

Wait.props

  • initialStatus

must be one of STATUS.WAITING, STATUS.SUCCEED, STATUS.FAILED, default to STATUS.WAITING

  • loader

a function or react node (anything can be renderd), if it is a function, it accept one argument which is provided by child when it call the start callback. check example for details.

the loader is only showed when status is STATUS.WAITING

  • errorContent

a function or react node (anything can be renderd), if it is a function, it accept one argument which is provided by child when it call the fail callback. check example for details

the loader is only showed when status is STATUS.FAILED

  • children

a function accepts 3 parameters:

  • wait - tell component Wait start waiting, status is STATUS.WAITING
  • succeed - tell component Wait status is STATUS.succeeded
  • fail - tell component Wait status is STATUS.failed

react-wait assures that children is only renderd/mounted once, so it is safe to start some asynchronous task in componentDidMount

CAVEAT!


you must make sure children don't show, eg. render null|false|'' when WAITING/FAILED, Wait will always show children.

A common pattern is only show some content when children does load the required data

Development

$ git clone [email protected]:xiechao06/react-wait.git
$ cd react-wait
$ npm ci
$ npm run dev # enter watching-testing mode

Build

$ cd react-wait
$ npm ci
$ npm run build

Run example

$ cd react-wait
$ npm run build
$ cd example
$ npm ci
$ npm start