jerry
jerry

Reputation: 155

Call function from within node event handler

I'm using icpRenderer to send messages in an electron app from the renderer to the main process. Below is the handler that listens for the messages. I'd like to call another function from within that handler. How can I bind this?

const onMessageReceived = (m: string) => {
  console.log(m);
};

ipcMain.on('my-custom-signal', (event, arg) => {
  this.onMessageReceived(arg);// how can I call this?
}); 

Upvotes: 2

Views: 280

Answers (1)

AzloD
AzloD

Reputation: 86

this is not the same inside the handler.

You can do this:

let that = this;
const onMessageReceived = (m: string) => {
  console.log(m);
};

ipcMain.on('my-custom-signal', (event, arg) => {
  that.onMessageReceived(arg); // how can I call this?
});

Upvotes: 2

Related Questions