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-queued-effect

v2.0.0

Published

React useQueuedEffect hook

Downloads

3

Readme

react-queued-effect

React useQueuedEffect hook that lets you to queue up effects and run them 1-by-1

Why useQueuedEffect?

useQueuedEffect is usefull whenever you need to run your effects one after another.

A realworld example: @react-pdf/renderer library (which is awesome library, by the way) allows you to generate PDF docs on the fly with React, but currently (Aug 2021) it fails to generate more than one document at a time. So if you need to generate more than 1 document on a page, you'd need to queue the docs up in order to render them one by one.

How to use it

The usage of this hook is quite simple and straightforward. The only gotcha you should be aware of is the "useCallback". It's strongly recommended to wrap your effects with the useCallback and respect effect's dependencies in order to avoid infinite update loops.

import { useCallback     } from 'react'
import { useQueuedEffect } from 'react-queued-effect'

function SomeComponent() {

    // (1) wrap your effect with useCallback
    const someEffect = useCallback(() => { /* do something here */ }, [ /* dependencies go here (if there are any) */ ])

    // (2) pass it to useQueuedEffect
    useQueuedEffect(someEffect)

    // (3) and that's it!
    return ( /* ... */ )
}

Example

This code illustrate how useQueuedEffect works with a bunch of components. The goal would be to update these components' state in a 1-by-1 fashion:

import   React             from 'react'
import   ReactDOM          from 'react-dom'
import { useCallback     } from 'react'
import { useQueuedEffect } from 'react-queued-effect'


function MyComponent({ id }) {

    const [ done, setDone ] = useState(false)

    // It's essential to wrap your queued effects with useCallback hook,
    // otherwise it's easy to end up with infinite loop of updates
    useQueuedEffect(useCallback(() => {

        // Here you can do some stuff that needs to be done 1-by-1.
        // This is just a dummy sample code that basically do nothing,
        // it just delays component's state update:

        console.log(`calling effect for id=${id}`)
        const delay = 1000

        // useQueuedEffect respects Promises as results of callback functions:
        return new Promise(resolve => {

            setTimeout(() => { setDone(true); resolve() }, delay)
        })

        // And don't lie to React on your callback's real dependencies:
    }, [id] ))

    return <span>{ done ? `ID:${id} is done` : `waiting for ID=${id}` }</span>
}


function App() {

    // Let's generate a bunch of test components
    // and see how they get updated nicely in a 1-by-1 manner

    const testCount = 1000

    return (
        <ul>
            { [...new Array(testCount)].map((_,n) => <li key={n}><MyComponent id={n}/></li>) }
        </ul>
    )
}


ReactDOM.render( <App />, document.getElementById('root') )