2

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?
}); 
2
  • 1
    remove this - unlike languages such as Java this.something() is completely different to something() Commented Jan 14, 2020 at 13:36
  • this works, thank you Commented Jan 15, 2020 at 2:26

1 Answer 1

2

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?
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.