Electron:将 IPC 消息从 React 发送到 Electron 主进程?

Electron: Send IPC messages from React to Electron main process?

假设我想构建一个简单的文件浏览器 Electron 应用程序,并使用 React 设计 UI。现在,我需要 React 将适当的 IPC 调用从组件发送到 Electron 的主进程,并接收包含由 React 呈现的文件资源管理器的文件名的回复。

我该怎么做?不幸的是,最近没有这方面的教程。

所以,@pilchard 的评论将我指向了一个名为 Creating standalone Desktop Applications with React, Electron and SQLite3 的博客 post,它最终给了我最好的答案。

你使用所谓的contextBridge,用它来创建一个api来桥接渲染器进程和主进程,并只公开需要的功能。阅读我上面提到的 post(向下滚动到“React 与 Electron 之间的通信”部分),您会明白的。