javascript - how to communicate between react and electron - Stack Overflow

Creating desktop application using react and electron.I want to call method in main.js of electron from

Creating desktop application using react and electron.I want to call method in main.js of electron from react ponent.In angular there was a npm package.

import React, { useState, useEffect, useRef } from 'react';
import './diagnosis.css';
const electron = window.require('electron');// if i use require('electron') throws error
function Diagnosis(props) {
    const [data, setData] = useState({ hits: [] });
    useEffect(() => {
        getExeFiles();
    });
    const getExeFiles = () => {
        electron.ipcRenderer.send('get-exe'); 
    }
    return(<></>)
}

main.js

electron.ipcMain.on('get-exe', () => {
    console.log('reaciovg');
    mainWindow.webContents.send('return-exe', '');
});

How to overe this issue?

Creating desktop application using react and electron.I want to call method in main.js of electron from react ponent.In angular there was a npm package.

import React, { useState, useEffect, useRef } from 'react';
import './diagnosis.css';
const electron = window.require('electron');// if i use require('electron') throws error
function Diagnosis(props) {
    const [data, setData] = useState({ hits: [] });
    useEffect(() => {
        getExeFiles();
    });
    const getExeFiles = () => {
        electron.ipcRenderer.send('get-exe'); 
    }
    return(<></>)
}

main.js

electron.ipcMain.on('get-exe', () => {
    console.log('reaciovg');
    mainWindow.webContents.send('return-exe', '');
});

How to overe this issue?

Share Improve this question edited Jan 12, 2020 at 12:08 frogatto 29.3k13 gold badges89 silver badges134 bronze badges asked Jan 10, 2020 at 11:47 KarthiKarthi 3,48912 gold badges39 silver badges56 bronze badges 3
  • At your main process, the reaciovg string is being shown correctly? – tpikachu Commented Jan 10, 2020 at 11:49
  • its just a console(to check whether the even is triggered). – Karthi Commented Jan 10, 2020 at 11:54
  • socket.io is a good alternative way. – abderrezague mohamed Commented Aug 30, 2023 at 13:23
Add a ment  | 

1 Answer 1

Reset to default 3

At your Renderer.js

const { ipcRenderer } = require('electron');

async function runCommand(cmd) {
  const res = await ipcRenderer.sendSync('runCommand', cmd);
  return res;
}

At you main.js

// Listen event through runCommand channel
// And return the result to Renderer.
ipcMain.on('runCommand', async (event, arg) => {
  event.returnValue = await runCommand(arg);
});

This is the simplest way to municate between main and renderer process.

But I think you are going to send the result from the main process to renderer using mainWindow.webContents.send('return-exe', '');

So this means, you are sending the result through return-exe IPC channel from main to renderer. And you should listen event from this channel at your renderer. Like this

ipcRenderer.on('retrun-exe', (event, arg) => {
    ...
});

You can add this listener at your lifecycle functions. I was used to add this to ponentDidMount() But in your case, please add this to your useEffect()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742395017a4435769.html

相关推荐

  • javascript - how to communicate between react and electron - Stack Overflow

    Creating desktop application using react and electron.I want to call method in main.js of electron from

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信