New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@shopify/react-web-worker

Package Overview
Dependencies
Maintainers
0
Versions
143
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@shopify/react-web-worker

A hook for using web workers in React applications

  • 5.1.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15K
increased by28.34%
Maintainers
0
Weekly downloads
 
Created
Source

@shopify/react-web-worker

Build Status Build Status License: MIT npm version npm bundle size (minified + gzip)

A hook for using web workers in React applications.

Installation

yarn add @shopify/react-web-worker

Usage

This package provides a useWorker hook to leverage web workers in React. For convenience, it also re-exports the entirety of @shopify/web-worker, so you only need to install this package to get access to those additional exports.

useWorker()

useWorker allows your React applications to take advantage of web workers provided by createWorkerFactory from @shopify/web-worker. This hook creates a web worker during render and terminates it when the component unmounts.

import React, {useEffect} from 'react';
import {Page} from '@shopify/polaris';
import {createWorkerFactory, useWorker} from '@shopify/react-web-worker';

// assume ./worker.ts contains
// export function hello(name) {
//  return `Hello, ${name}`;
// }

const createWorker = createWorkerFactory(() => import('./worker'));

function Home() {
  const worker = useWorker(createWorker);
  const [message, setMessage] = React.useState(null);

  useEffect(() => {
    (async () => {
      // Note: in your actual app code, make sure to check if Home
      // is still mounted before setting state asynchronously!
      const webWorkerMessage = await worker.hello('Tobi');
      setMessage(webWorkerMessage);
    })();
  }, [worker]);

  return <Page title="Home"> {message} </Page>;
}

You can optionally pass a second argument to useWorker, which will be used as the options to the worker creator function.

FAQs

Package last updated on 24 Sep 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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc