I have a problem with the ipcRenderer not receiving a message from ipcMain when my app is initializing.
I’m running a 3rd party component from which I want to receive some settings. Once the variables are stored in a state variable within my home component, I’m rendering a certain div.
When logging the events, I can see that main.js is receiving the request from the ipcRenderer and when printing the settings I can see the data. However my window is not receiving the answer. Sometimes when I relaunch the app it works but right now it is not reliable and I don’t really understand why.
Main.js:
ipcMain.on('getSettings', () => {
console.log('getSettings was triggered.')
console.log(settings.store.store)
mainWindow.webContents.send('allSettings', settings.store.store)
});
Home.js:
import React, { useState, useEffect } from 'react';
const { ipcRenderer } = electron;
const Home = () => {
const [settings, setSettings] = useState(null);
const [startedUp, setStartedUp] = useState(false);
useEffect(() => {
ipcRenderer.on('allSettings', (event, data) => { loadSettings(data); });
ipcRenderer.send('getSettings');
return () => {
ipcRenderer.removeAllListeners();
};
}, [])
useEffect(() => {
if (settings != null) {
setStartedUp(true);
}
}, [settings])
function loadSettings(data) {
console.log('loading settings...')
if (data != null) {
setSettings(data);
console.log('settings loaded: ', data)
}
}
return (
<>
{startedUp ?
<div className='content'>
Content
</div>
:
<div className='empty'>
</div>}
</>
);
};
export default Home;
As stated above, sometimes when I relaunch the App a couple of times it works fine but most of the time it doesn't. I'm sure that I'm missing something fundamental here but I cant wrap my head around.
Hope somebody is able to help. Cheers!