Socket
Book a DemoInstallSign in
Socket

react-webworker-hook

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-webworker-hook

A react custom hook to deal with webworkers

1.0.0
latest
Source
npmnpm
Version published
Weekly downloads
42
Maintainers
1
Weekly downloads
 
Created
Source

react-webworker-hook

A react custom hook to communicate with web worker. just specify the web worker url or a script that you want webworker to run and you will get access to any message, error it sends and postMessage handler which is used to communicate with web worker

Installation

npm install --save react-webworker-hook

Usage

a quick sample here https://codesandbox.io/p/sandbox/react-webworker-hook-example-6xt8rm

import React, { useState } from "react";
import useWebWorker from "react-webworker-hook";

function GenerateFibonacci() {
  const [data = 0, postData] = useWebWorker({
    url: "./webworker_example.js"
  });
  const [count, setCount] = useState(0);

  return (
    <div>
      {`fibonacci for ${count}: ${data}`}
      <button
        onClick={() => {
          setCount(count + 1);
          postData(count);
        }}
      >
        Generate
      </button>
    </div>
  );
}

export default GenerateFibonacci;

It also comes with other helper functions like when we have a small script which should be deployed into webworker

const [data, postData, error] = useWebWorkerFromScript(`
const fib = n => (n < 2 ? n : fib(n - 1) + fib(n - 2));

onmessage = ({ data }) => {
  postMessage(fib(data));
};

`)

pass url of a web worker, webworker should be served from the same origin

const [data, postData, error] = useWebWorkerFromUrl('./webworker_example.js');

when we have an existing worker and it should be used through hook

const [data, postData, error] = useWebWorkerFromWorker('./webworker_example.js');

API

useWebWorker takes following arguments

a config object with either url or worker object

useWebWorker provides following values

  • data - data which has been sent by webworker
  • postMessage - handler which is used to communicatew with webworker
  • error - which will contain an error object if thrown by web worker

Keywords

react

FAQs

Package last updated on 24 Jan 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.