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

@dev-plus-plus/react-await

v2.1.2

Published

A loading indicator react component based on Promises.

Downloads

53

Readme

React Await

A loading indicator react component based on Promises.

The structure uses Context and Hookers in order to use it in different parts of code.

Features

  • Good functionality with hookers and consumer components
  • Fade effects preset
  • Customizable fade effects
  • Customizable loaders
  • Handling error scenarios
  • Full typescript integration

Getting Started

npm i @dev-plus-plus/react-await react react-dom react-transition-group

or simply

npm i @dev-plus-plus/react-await

Optionally, you may include the custom fade effects

import '@dev-plus-plus/react-await/styles/effect.css'

Basic Usage

First, you need to include <AwaitProvider> to state a context. Everything inside can be triggered by useAwait hooker. The visual effect is affected by <AwaitConsumer>

Normally, <AwaitProvider> stay in the App file:

import '@dev-plus-plus/react-await/styles/effect.css'

import {AwaitProvider, AwaitConsumer, useAwait} from '@dev-plus-plus/react-await'

function App() {
  //...

  return (
    <AwaitProvider>
      <AnyOtherComponent />
    </AwaitProvider>
  )
}

function AnyOtherComponent() {
  const {loader, controller} = useAwait('fake') // set the name of consumer

  const runFakeSuccess = async () => {
    await new Promise((resolve) => setTimeout(resolve, 2000))
  }

  const runFakeError = async () => {
    await new Promise((_resolve, reject) => setTimeout(reject, 2000))
  }

  const renderButtons = () => (
    <div>
      <button onClick={() => controller.run(runFakeSuccess)}>Fake Success</button>
      <button onClick={() => controller.run(runFakeError)}>Fake Error</button>
    </div>
  )

  return (
    <>
      State: {loader.state}

      <AwaitConsumer
        loader={loader} // required
        animationClassName={'fade'} // default fade-blur
        animationDuration={400} // default 400
        onLoadingStart={() => alert('Triggered event - loading start')}
        onLoadingEnd={() => alert('Triggered event - loading end')}
        onError={() => alert('Triggered event - error')}
        loadingView={<div>Now loading...</div>}
        errorView={<div>{renderButtons()}Error!</div>}
        style={{width: 300, backgroundColor: '#eee'}}
      >
        {renderButtons()}

        <div>This content will be load</div>
      </AwaitConsumer>
    </>
  )
}

Set a default loader and fade effects

This library doesn't include loaders, but I strongly recommended to use react-spinners.

You may also determine the default fade effects presets or use your own. The available effects are: fade, fade-blur, fade-up, fade-down, fade-left, fade-right. Default is fade-blur.

The duration of effects can be also changed, but you have to change both in effect css file and defaultAnimationDuration prop. Default is 400.

Here is an example of a customization:

import '@dev-plus-plus/react-await/styles/effect.css'

import {AwaitProvider, AwaitConsumer, useAwait} from '@dev-plus-plus/react-await'
import {BarLoader} from 'react-spinners'

function App() {
  //...

  return (
    <AwaitProvider
      defaultLoadingView={<BarLoader />}
      defaultAnimationClassName={'fade'}
      defaultAnimationDuration={400}
    >
      ...
    </AwaitProvider>
  )
}

useAwait methods and properties

Usage

useAwait(name, index)

| name | index (optional) | |---|---| | The name of consumer | The index of consumer in case the consumer is inside a list |

record prop

Returns a dictionary of all states registered. The key is the name of the consumer and the value is the current state.

laoder.state prop

Returns the current state of loader represented by enum. The available states are: LOADING, STANDBY, ERROR. If the state is undefined that means the consumer never started.

loader.fullName prop

Returns the full name of consumer. Normally, the full name is the real name. The exception is when the index is set.

loader.isUnset prop

Returns true if the state is undefined

loader.isStandby prop

Returns true if the state is Standby

loader.isLoading prop

Returns true if the state is Loading

loader.isError prop

Returns true if the state is Error

controller.init method

Starts to load.

Example:

// ...
const {controller} = useAwait('test')

useEffect(() => {
  controller.init() // start to load when the component mounts
}, [])
// ...

controller.done method

Finish the loading.

Example:

// ...
const {controller} = useAwait('test')

useEffect(() => {
  run()
}, [])

async function run() {
  controller.init()
  await new Promise((resolve) => setTimeout(resolve, 1000))
  controller.done() // end to load after 1 second
}
// ...

controller.error method

Finish the loading with error.

Example:

// ...
const {controller} = useAwait('test')

useEffect(() => {
  run()
}, [])

async function run() {
  controller.init()
  await new Promise((resolve) => setTimeout(resolve, 1000))
  controller.error() // end to load with after 1 second
}
// ...

loader.run method

Starts to load, then finish the loading with success or error state.

Example:

// ...
const {controller} = useAwait('test')

useEffect(() => {
  controller.run(() => Promise.resolve(), delay = 1000)
}, [])
// ...