$ npm install @koale/useworker
< 1KB
, with zero
dependenciesnpm install --save @koale/useworker
import { useWorker, WORKER_STATUS } from "@koale/useworker";
Before you start using this hook, I suggest you to read the Web Worker documentation.
import React from "react";
import { useWorker } from "@koale/useworker";
const numbers = [...Array(5000000)].map(e => ~~(Math.random() * 1000000));
const sortNumbers = nums => nums.sort();
const Example = () => {
const [sortWorker] = useWorker(sortNumbers);
const runSort = async () => {
const result = await sortWorker(numbers); // non-blocking UI
console.log("End.");
};
return (
<button type="button" onClick={runSort}>
Run Sort
</button>
);
};
More examples: https://github.com/alewin/useWorker/tree/develop/example
useWorker
functionuseWorker
functionThe library is experimental so if you find a bug or would like to request a new feature, open an issue
MIT © alewin
© 2010 - cnpmjs.org x YWFE | Home | YWFE