如何在 react/webpack 2 设置中导入电子 ipcRenderer

IT技术 reactjs webpack electron
2021-04-28 09:19:17

使用电子、react (es6 / jsx)、sass、pouchdb 和 webpack 2 设置。我无法导入或要求 ipcRenderer 使主进程和渲染器进程之间的通信成为可能。我的设置可以在这里找到:https : //github.com/wende60/timeTracker

任何提示如何将 ipcRenderer 放入react组件中?

干杯,乔

4个回答
const electron = window.require('electron');
const ipcRenderer  = electron.ipcRenderer;

我认为这是更好的解决方案,因为它避免了弹出 React 应用程序。

我有同样的问题。这为我解决了这个问题:

添加在webpack.config.js

const webpack = require("webpack");

module.exports = {
    plugins: [
        new webpack.ExternalsPlugin('commonjs', [
            'electron'
        ])
    ]
    ...
}

然后你可以使用它

import {ipcRenderer} from "electron";

我建议你在这里阅读我的回复

您需要像这样设置您的应用程序:

主文件

const {
  app,
  BrowserWindow,
  ipcMain
} = require("electron");
const path = require("path");
const fs = require("fs");

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win;

async function createWindow() {

  // Create the browser window.
  win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: false, // is default value after Electron v5
      contextIsolation: true, // protect against prototype pollution
      enableRemoteModule: false, // turn off remote
      preload: path.join(__dirname, "preload.js") // use a preload script
    }
  });

  // Load app
  win.loadFile(path.join(__dirname, "dist/index.html"));

  // rest of code..
}

app.on("ready", createWindow);

ipcMain.on("toMain", (event, args) => {
  fs.readFile("path/to/file", (error, data) => {
    // Do something with file contents

    // Send result back to renderer process
    win.webContents.send("fromMain", responseObj);
  });
});

预加载.js

** 更新:不要使用send键值作为属性名称。win.webContents.send当您尝试win.webContents.send('your_channel_name')在主进程中调用时,它将覆盖方法并且什么也不做main.js最好使用更好的名称,例如requestresponse

const {
    contextBridge,
    ipcRenderer
} = require("electron");

// Expose protected methods that allow the renderer process to use
// the ipcRenderer without exposing the entire object
contextBridge.exposeInMainWorld(
    "api", {
        //send: (channel, data) => {
        request: (channel, data) => {
            // whitelist channels
            let validChannels = ["toMain"];
            if (validChannels.includes(channel)) {
                ipcRenderer.send(channel, data);
            }
        },
        //receive: (channel, func) => {
        response: (channel, func) => {
            let validChannels = ["fromMain"];
            if (validChannels.includes(channel)) {
                // Deliberately strip event as it includes `sender` 
                ipcRenderer.on(channel, (event, ...args) => func(...args));
            }
        }
    }
);

索引.html

<!doctype html>
<html lang="en-US">
<head>
    <meta charset="utf-8"/>
    <title>Title</title>
</head>
<body>
    <script>
        window.api.response("fromMain", (data) => {
            console.log(`Received ${data} from main process`);
        });
        window.api.request("toMain", "some data");
    </script>
</body>
</html>

截至 2020 年 5 月,我认为Erik Martín Jordán 说得最好

创建一个 preload.js 文件:

window.ipcRenderer = require('electron').ipcRenderer;

在 main.js 上:


// Create the browser window.
mainWindow = new BrowserWindow({
    alwaysOnTop: true,
    frame: false,
    fullscreenable: false,
    transparent: true,
    titleBarStyle: 'customButtonsOnHover',
    show: false,
    width: 300, 
    height: 350,
    webPreferences: {
        // UPDATE: for electron > V12 consider setting contextIsolation and see: https://github.com/electron/electron/issues/9920#issuecomment-797491175
        nodeIntegration: true,
        preload: __dirname + '/preload.js'
    }
});

// Blur window when close o loses focus
mainWindow.webContents.on('did-finish-load', () => mainWindow.webContents.send('ping', '🤘') );

此文件上的 mainWindow 变量将预加载 preload.js 文件。现在 React 组件可以调用 window.ipcRenderer 方法。

在 React app.js 中:

import React, { useEffect, useState } from 'react';
import './App.css';

function App() {
    
    useEffect( () => {
        
        window.ipcRenderer.on('ping', (event, message) => { 
            console.log(message) 
        });
                
    }, []);
            
    return (
    <div className = 'App'></div>
    );
}

export default App;