@shopify/react-web-worker
A hook for using web workers in React applications
Last updated 12 days ago by shopify-dep .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @shopify/react-web-worker 
SYNC missed versions from official npm registry.

@shopify/react-web-worker

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.

Current Tags

  • 1.2.21                                ...           latest (12 days ago)

31 Versions

  • 1.2.21                                ...           12 days ago
  • 1.2.20                                ...           22 days ago
  • 1.2.19                                ...           a month ago
  • 1.2.18                                ...           a month ago
  • 1.2.16                                ...           2 months ago
  • 1.2.15                                ...           2 months ago
  • 1.2.14                                ...           2 months ago
  • 1.2.13                                ...           2 months ago
  • 1.2.12                                ...           3 months ago
  • 1.2.11                                ...           3 months ago
  • 1.2.10                                ...           3 months ago
  • 1.2.9                                ...           3 months ago
  • 1.2.8                                ...           3 months ago
  • 1.2.7                                ...           4 months ago
  • 1.2.6                                ...           5 months ago
  • 1.2.5                                ...           6 months ago
  • 1.2.4                                ...           6 months ago
  • 1.2.3                                ...           7 months ago
  • 1.2.2                                ...           8 months ago
  • 1.2.1                                ...           8 months ago
  • 1.2.0                                ...           8 months ago
  • 1.1.0                                ...           8 months ago
  • 1.0.4                                ...           8 months ago
  • 1.0.3                                ...           8 months ago
  • 1.0.2                                ...           8 months ago
  • 1.0.1                                ...           9 months ago
  • 1.0.0                                ...           9 months ago
  • 0.0.4                                ...           9 months ago
  • 0.0.3                                ...           9 months ago
  • 0.0.2                                ...           9 months ago
  • 0.0.1                                ...           9 months ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 8
Last Month 14
Dependencies (3)
Dev Dependencies (0)
None
Dependents (1)

Copyright 2014 - 2017 © taobao.org |