简体   繁体   English

只有在所有组件都准备就绪后,如何从电子发送事件到反应组件?

[英]How to send events from electron to react components only after all components are ready?

I have an electron app and I'm trying to use ReactJs with it.我有一个电子应用程序,我正在尝试将 ReactJs 与它一起使用。 I have code in main.js that sends some data on dom-ready event:我在main.js中有代码发送一些关于dom-ready事件的数据:

mainWindow.webContents.on('dom-ready', function () {
    dao.getIncomes(function (data) {
        mainWindow.webContents.send('income-data', data);
    });
}

Then I have React component, where I'm trying to set a listener for this event:然后我有 React 组件,我正在尝试为这个事件设置一个监听器:

useEffect(() => {
    ipcRenderer.on('income-data', function (event, data) {
        // some code
    });
}, []);

Sometimes, a component init this effect only when the event has already sent.有时,组件仅在事件已经发送时才初始化此效果。 And listener doesn't receive this data.并且侦听器不会收到此数据。

How do I set the listener correctly in this case?在这种情况下如何正确设置侦听器? O may be I should send events later, on some other event, instead of dom-ready O 可能是我应该稍后在其他事件上发送事件,而不是dom-ready

Thank you.谢谢你。

I think first you should tell the electron the function in effect is being Execute。我想首先你应该告诉电子这个函数实际上是被执行的。

const { ipcMain } = require('electron')
ipcMain.on('react-effect-executed', (event, arg) => {
  dao.getIncomes(function (data) {
    mainWindow.webContents.send('income-data', data);
  });
})

useEffect(() => {
    ipcRenderer.send('react-effect-executed', '');
    ipcRenderer.on('income-data', function (event, data) {
        // some code
    });
}, []);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在页面重新加载事件后保留带有子项的 React 组件? - How to Persist React Components with Children after Page Reload Events? 在主要组件中响应本机获取数据并发送到所有其他组件 - react native fetch data in main components and send to all other components 在 React / testing-library/react 中呈现所有组件后,如何从表中获取元素? - How to get the elements from a table after all components are rendered in React / testing-library/react? 如何在 React 中将信息从组件传递到组件? - How to pass information from components to components in React? 反应/电子渲染组件失败 - React / Electron Rendering Components Is Failing 如何通过上下文将数据从一个组件发送到另一个组件? - how to send data From one component to another components with context in react? 我如何等待子组件使用 React 钩子“准备好”? - How do I wait for child components to be “ready” with React hooks? 如何基于其他组件的事件更新组件URL - How to update components URL based on events from other components 在 React 中向子组件传播事件 - Propogating Events to Child Components in React 在React组件之间交换事件 - Exchanging events between React components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM