我正在构建基于React和Redux的Electron应用程序。我从electron-react-boilerplate开始,这非常简约且易于理解。
我希望用户在Electron菜单上打开一个文件,因此,我想调用reducer并更改Redux应用程序状态。非常简单的概念。
问题是我不知道如何从我的根组件外部更改Redux状态。电子菜单在main.js file中定义。根组件在index.js file中与Redux state
(store
变量)一起定义。
在main.js
文件中,我想做类似的事情:
submenu: [{
label: '&Open',
accelerator: 'Ctrl+O',
click: function() {
// I want to change my app Redux state here. But I don't know how.
}
}
有什么想法吗?
答案 0 :(得分:23)
您可以在主进程中获取文件名,然后通过Electron IPC将其发送到渲染器进程,例如:
在main.js
// mainWindow = new BrowserWindow();
submenu: [{
label: '&Open',
accelerator: 'Ctrl+O',
click: () => {
// popup a dialog to let the user select a file
// ...
// then send the filename to the renderer process
mainWindow.webContents.send('open-file', selectedFilename);
}
}]
在index.js
import { ipcRenderer } from 'electron';
ipcRenderer.on('open-file', (event, filename) => {
store.dispatch({ type: 'OPEN_FILE', filename });
});
答案 1 :(得分:3)
另一个选项是使用index.js
模块在渲染器端(remote
)构建菜单,然后您可以直接从点击回调中调用调度程序。