⚛️useWorker() React Hook: Running heavy task in background, without blocking the UI

栏目: IT技术 · 发布时间: 4年前

内容简介:Use web workers with react hookBefore you start using thisMore examples:

⚛️useWorker() React Hook: Running heavy task in background, without blocking the UI

Use web workers with react hook

https://useworker.js.org/

:art: Features

  • Run expensive function without blocking UI ( Show live gif )
  • Supports Promises pattern instead of event-messages
  • Size: < 1KB , with zero dependencies
  • Clear API using hook

:floppy_disk: Install

npm install --save @koale/useworker

:hammer: Import

import { useWorker, WORKER_STATUS } from "@koale/useworker";

:orange_book: Documents

:bread: Demo

⚙ Web Workers

Before you start using this hook , I suggest you to read the Web Worker documentation.

:feet: Usage

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>
  );
};

:feet: Examples

More examples: https://github.com/alewin/useWorker/tree/develop/example

:wrench: Roadmap

  • import and use external script inside useWorker function
  • import and use local script inside useWorker function
  • run multiple instance of the worker

:earth_asia: Contribute? Bug? New Feature?

The library is experimental so if you find a bug or would like to request a new feature , open an issue

:bulb: Similar Project

:scroll: License

MIT © alewin


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算机真实感图形的算法基础

计算机真实感图形的算法基础

彭群生 / 科学出版社 / 1999-6 / 45.00元

《计算机真实感图形的算法基础》是系统介绍计算机真实感图形基础理论与算法的一本专著。全书共分九章,包括图形学基础、光照模型原理、简单画面绘制算法、光线跟踪、纹理映射、阴影生成、光能辐射度方法、实时图形绘制技术、自然景物仿真、颜色等。《计算机真实感图形的算法基础》的特点是内容全面,取材新颖,注重算法,力求实用。除系统叙述计算机真实感图形生成的基本概念、基本算法外,作者还注意结合亲身实践体会介绍国内外的......一起来看看 《计算机真实感图形的算法基础》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

在线进制转换器
在线进制转换器

各进制数互转换器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具