Easy function to set up workers
scriptPath: String: Path to the script file that a new Worker is to be created withworkerOptions: Object:onMessageandonMessageErroroptions can be passed to communicate with the workerattributes: Object: Event handlers to attach to the worker
worker: Worker: The worker instance
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;
}