electronreact-typescriptelectron-react-boilerplate

Electron ipcRenderer.sendSync() returning undefined


I'm writing an app with the electron-react-boilerplate framework.

I've been following a tutorial here: https://riptutorial.com/electron/example/19713/synchronous-ipc-communication

In my renderer process I have:

let a = window.electron.ipcRenderer.sendSync('LIST', []);
console.log(a);

and in my main process:

ipcMain.on('LIST', (event) => {
  event.returnValue = 'test';
});

However the value 'a' is undefined when it is logged. What am I doing wrong?


Solution

  • I realised what I did wrong.

    In preload.ts I exposed the function sendSync like below:

    sendSync(channel: Channels, args: unknown[]) {
      ipcRenderer.sendSync(channel, args);
    },
    

    ...this didn't return anything, hence the undefined. I should have done:

    sendSync(channel: Channels, args: unknown[]) {
      return ipcRenderer.sendSync(channel, args);
    },
    

    Now it returns as expected!