简体   繁体   中英

Electron react sending data two-way in ipcMain and ipcRenderer

I am trying to create a communication between ipcMain and ipcRenderer in electron. I am using electron react boilerplate

So first step was adding this part to main:

app
  .whenReady()
  .then(() => {
    ipcMain.handle('dialog:openFile', () => {
      console.log('test');
    });

second step was to add it in preload file:

contextBridge.exposeInMainWorld('electronAPI', {
  openFile: () => ipcRenderer.invoke('dialog:openFile'),
});

and the third step is where I get stuck. I am trying to add it in react component by:

await window.electronAPI.openFile();

But I get Property 'electronAPI' does not exist on type 'Window & typeof globalThis'. error. What is the issue here?

Hello @Webby are you sure that the preload file is loaded in the browserWindow Object? Also that is necessary that you have the property contextIsolation on True

something like this

const win = new BrowserWindow(
{
  fullscreen: false,
  webPreferences: {
    contextIsolation: true,
    preload: path.join(__dirname, '../path/to/your/preload.js'),
  },
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM