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

@okaygift/react-use-worker

v1.0.0

Published

A simple React hook to run expensive functions in a background thread using Web Workers, without blocking the UI.

Downloads

4

Readme

React Use Worker A simple React hook to run expensive functions in a background thread using Web Workers, preventing the UI from freezing.

The Problem In a standard React application, all JavaScript code runs on a single "main thread." If you need to perform a CPU-intensive task—like processing a large dataset, performing complex calculations, or parsing a large file—it will block this main thread. While the task is running, the user's browser will become completely unresponsive. They won't be able to click buttons, scroll, or interact with your app in any way.

The Solution Web Workers provide a way to run JavaScript in a background thread, separate from the main UI thread. However, the standard API for using them (postMessage, onmessage) is event-based and doesn't fit nicely into the declarative, hook-based model of React.

useWorker is a simple hook that wraps the complexity of Web Workers in a familiar, hook-based API. It allows you to run a function in the background and receive its result as state.

Installation npm install @your-username/react-use-worker

How to Use The useWorker hook takes a function and an array of its arguments. It returns an object containing the status of the worker, the data it returned, or any error that occurred.

import { useWorker } from '@your-username/react-use-worker';

// A simple (but potentially slow) function to find prime numbers. // IMPORTANT: This function must be self-contained and not use any variables // from its surrounding scope. const findPrimes = (limit: number) => { const primes = []; for (let i = 2; i <= limit; i++) { let isPrime = true; for (let j = 2; j < i; j++) { if (i % j === 0) { isPrime = false; break; } } if (isPrime) { primes.push(i); } } return primes; };

function PrimeCalculator() { const [limit, setLimit] = useState(10000);

// Run the findPrimes function in a background thread. // The worker will re-run automatically if the limit dependency changes. const { data, error, status } = useWorker(findPrimes, [limit]);

return ( Prime Number Calculator <input type="number" value={limit} onChange={(e) => setLimit(Number(e.target.value))} />

  {status === 'running' && <p>Calculating...</p>}
  {status === 'error' && <p>Error: {error?.message}</p>}
  {status === 'success' && (
    <p>Found {data?.length} primes up to {limit}.</p>
  )}
</div>

); }

API useWorker(fn, fnArgs) Parameters fn: (...args: any[]) => T (required): The function to execute in the worker. Crucially, this function must be pure and self-contained. It cannot access any variables outside of its own scope (no closures).

fnArgs: any[] (required): An array of arguments to pass to the worker function. The hook will re-run the worker whenever the stringified version of this array changes.

Returns An object { data, error, status }:

data?: T: The successful result returned from your function. undefined otherwise.

error?: Error: An Error object if the function throws an error. undefined otherwise.

status: 'idle' | 'running' | 'success' | 'error': The current status of the worker.