saltyshiomix / nextron

⚡ Next.js + Electron ⚡
https://npm.im/nextron
MIT License
3.69k stars 215 forks source link

IpcRenderer.Invoke #418

Open footcoderr opened 8 months ago

footcoderr commented 8 months ago

Is there a reason why you didn't add 'invoke' on ipc in preload?

I've been using it useful because I've requested and received a response from the data. Is there a better way to do this?

aramrw commented 8 months ago

You can just add it yourself. I used it in my project here, idk if this is what you're asking but here's my code

Preload.ts:

import { contextBridge, ipcRenderer, BrowserWindow } from 'electron'

contextBridge.exposeInMainWorld('ipcRenderer', {
  invoke: (channel: string, ...args: any[]): Promise<any> => ipcRenderer.invoke(channel, ...args),

  removeAllListeners: (channel: string): Electron.IpcRenderer => ipcRenderer.removeAllListeners(channel),

  reload: (channel: string): Promise<any> => ipcRenderer.invoke('reload-window'),

})

In your react component:

const handleDeleteButton = () => {
    window.ipcRenderer.invoke("reload-window", projectName);
  };

In background.ts:

ipcMain.handle('reload-window', async (_event, _args) => {
  mainWindow?.reload();
});
bm777 commented 3 months ago

@footcoderr did you find a solution ?