Skip to content

Instantly share code, notes, and snippets.

@Blezzoh
Last active November 25, 2019 18:34
Show Gist options
  • Select an option

  • Save Blezzoh/600aad1836f2e3b008956608a7a4c9e0 to your computer and use it in GitHub Desktop.

Select an option

Save Blezzoh/600aad1836f2e3b008956608a7a4c9e0 to your computer and use it in GitHub Desktop.
example of adding your data to the view
componentDidMount() {
// instantiating the worker and adding a listener
this.worker = new Webworker(worker);
this.worker.addEventListener("message", e => {
const { data } = e;
this.setState({ data, isLoading: false });
});
}
onClick = () => {
// your event handler that will use a worker
const { numberOfUsers } = this.state;
if (Number.parseInt(numberOfUsers) >= 0) {
this.setState({ isLoading: true });
this.worker.postMessage({ numberOfUsers });
}
};
componentWillUnmount(){
// terminate the worker once you're done using it
this.worker.terminate()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment