如何根据电子菜单单击更改Redux状态?

时间:2016-02-20 21:54:43

标签: javascript reactjs redux electron

我正在构建基于React和Redux的Electron应用程序。我从electron-react-boilerplate开始,这非常简约且易于理解。

我希望用户在Electron菜单上打开一个文件,因此,我想调用reducer并更改Redux应用程序状态。非常简单的概念。

问题是我不知道如何从我的根组件外部更改Redux状态。电子菜单在main.js file中定义。根组件在index.js file中与Redux statestore变量)一起定义。

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.
    }
  }

有什么想法吗?

2 个答案:

答案 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)构建菜单,然后您可以直接从点击回调中调用调度程序。