我正在基于React和Redux构建一个Electron应用程序。我从electron-react-boilerplate开始,它非常简单并且易于理解。

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

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

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

任何想法?

最佳答案

您可以在主进程中获取文件名,然后通过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 });
});

关于javascript - 如何基于 Electron 菜单单击更改Redux状态?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35529532/

10-09 20:53