Skip to content

Simplest way to include web workers with your React Application and get smoother experience

Notifications You must be signed in to change notification settings

zonayedpca/react-web-workers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

90 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-web-workers

A Simple Library to use web worker with your React Application Easily

Intro

If you are using some expensive processes inside your webpage, make sure you don't compromise your performance there. Web workers can make your web page feel smooth and realistic

Install

Installing this package is super simple and just like any other package. If you are using npm:

npm i react-web-workers

But if you are yarn lover:

yarn add react-web-workers

Workers

Create as many workers as you want as a function and export them just like this:

const message = () => {
  let text = 'Hello I am Your Web Worker'

  const sentData = () => {
    // eslint-disable-next-line no-restricted-globals
    self.postMessage({ text })
  }

  // eslint-disable-next-line no-restricted-globals
  self.onmessage = ({ data }) => {
    console.log(data)
    sentData()
  }
}

export default message;

Work with Workers

Once your workers are ready, import react-web-workers

import WebWorker from 'react-web-workers';

Also import your workers:

import message from './workers/message';
...

Pass the workers directly as an array:

const [messageWorker, ...] = WebWorker([message, ...]);

Now you have access to all the web workers functionality with this messageWorker and all the other workers you've been working with

In case if you're feeling any difficulty, please refer to the example we have in this project.

Features

  • Simple to Use
  • Just import and start using
  • All the default functionality is kept
  • Create your worker function and export it

Examples

License

MIT © Zonayed Ahmed

About

Simplest way to include web workers with your React Application and get smoother experience

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published