react-recipes
Version:
A React Hooks utility library containing popular customized hooks
35 lines (26 loc) • 750 B
Markdown
Easy function to set up workers
- `scriptPath: String`: Path to the script file that a new Worker is to be created with
- `workerOptions: Object`: `onMessage` and `onMessageError` options can be passed to communicate with the worker
- `attributes: Object`: Event handlers to attach to the worker
- `worker: Worker`: The worker instance
```js
import { useWorker } from "react-recipes";
function App() {
const [value, setValue] = useState(0);
useWorker('/worker.js', {
onMessage: (e) => {
console.log('message received from worker');
console.log(e.data);
setValue(e.data);
},
onMessageError: (e) => {
console.log(e);
},
});
return value;
}
```