useWorker
Aboutβ
Worker hook for React.
Installationβ
npm install --save rooks
Importing the hookβ
import { useWorker } from 'rooks';
Usageβ
function Demo() {
const [value, setValue] = useState(0);
const [error, setError] = useState(null);
const worker = 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;
}
const rootElement = document.getElementById("root");
ReactDOM.render(<Demo />, rootElement);
Argumentsβ
Arguments | Type | Description | Default value |
---|---|---|---|
scriptPath | string | Path to the script file that a new Worker is to be created with | undefined |
options | Object | Options object within which onMessage and onMessageError options can be passed to communicate with the worker | {onMessage: () => {},,onMessageError: () => {}} |
Returned Objectβ
The worker instance is returned.
Join Bhargav's discord serverβ
You can click on the floating discord icon at the bottom right of the screen and talk to us in our server.