
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
react-usewebworker-hook
Advanced tools
Streamline React render times by moving computation heavy functions off the main thread with a simple hook
Wrap web workers in a React hook to use in your components. react-usewebworker-hook allows the developer to offload heavy computation that might lock up the main thread, which helps preserve a seamless user experience.
Returns the most recent web worker result along with a 'pending', 'resolved', 'stale', or 'rejected' status of the result's computation.
npm install react-usewebworker-hook
heftyFunctionWorker.ts
import { registerWorker } from "react-worker";
const computationHeavyFunction = (input: {
arg1: string,
arg2: string,
}): number => {
let sum = 0;
const limit = 200000000;
for (let i = 0; i < limit; i++) {
if (i % 2 === 0) {
sum += i;
}
}
return sum;
};
registerWorker(computationHeavyFunction);
const createHeftyFunctionWorker = () =>
new Worker(new URL("./heftyFunctionWorker.ts", import.meta.url));
useHeftyFunction.ts
import { useWebWorker } from "react-usewebworker-hook";
import { useCallback, useMemo } from "react";
const createHeftyFunctionWorker = () =>
new Worker(new URL("./heftyFunctionWorker.ts", import.meta.url));
export default function useHeftyFunction() {
const arg1 = "foo";
const arg2 = "bar";
const workerCallback = useCallback(createHeftyFunctionWorker, []);
const memoizedArgs = useMemo(() => ({ arg1, arg2 }), []);
const { status, result, error } = useWebWorker(workerCallback, memoizedArgs);
console.log(status, result, error);
return result;
}
Optionally, you can provide type arguments to useWebWorker if you want your result to be strongly typed. There is no type guard, so type safety of the result relies on the implementation of the underlying worker function.
import { useWebWorker } from 'react-usewebworker-hook';
import { useCallback, useMemo } from 'react';
const createHeftyFunctionWorker = () => new Worker(new URL('./heftyFunctionWorker.ts', import.meta.url));
type HeftyFunctionArgs = {
arg1: string;
arg2: string;
};
type HeftyFunctionResult = number;
export function useStronglyTypedHeftyFunction() {
const arg1 = 'foo';
const arg2 = 'bar';
const workerCallback = useCallback(createHeftyFunctionWorker, []);
const memoizedArgs = useMemo(() => ({ arg1, arg2 }), []);
const { status, result, error } = useWebWorker<HeftyFunctionArgs, HeftyFunctionResult>(workerCallback, memoizedArgs);
console.log(status, result, error);
return result;
}
If you are having an issue with the existing project code, please submit a bug report under the following guidelines:
Would love to hear about idea for improved features and functionality!
If you have developed a patch, bug fix, or new feature that would improve this library, please submit a pull request. Remember that this project is licensed under the MIT license, and by submitting a pull request, you agree that your work will be too.
FAQs
Streamline React render times by moving computation heavy functions off the main thread with a simple hook
The npm package react-usewebworker-hook receives a total of 1 weekly downloads. As such, react-usewebworker-hook popularity was classified as not popular.
We found that react-usewebworker-hook demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.