Reputation: 13723
I am making an electron app in which I am using a class to create instances of Renderer windows (I need multiple instances of the renderer). Each renderer window has to be connected to a specific data set.
The problem is, I can't see an obvious way in Electron to pass setup values to the renderer process when I set it up in the Main process. I am using the createWindow function and I don't see any way to connect any variable or object to it which can be recovered in the preload or the renderer Javascript file.
How do people typically handle that?
To illustrate further what I want to do.
How do I pass the dataset info to the new window when I use the createWindow command to create it?
Thanks in advance.
Upvotes: 0
Views: 457
Reputation: 1
I found that if I created a renderer and had the main process send a message immediately, the message would sometimes be lost. So I have found it necessary to wait for the 'did-finish-load' event first.
//main
const createWindow = (windowData) => {
let win = new BrowserWindow({
show: false,
});
win.loadFile(path.join(__dirname, 'index.html'))
win.webContents.once('did-finish-load', () => {
win.webContents.send('doThing', windowData)
})
}
Upvotes: 0
Reputation: 5332
Send each BrowserWindow their data like this:
// Main process.
win.webContents.send('data', {'a': 'b'... });
Recieve the data like this:
// Renderer process.
var ipcRenderer = require('electron').ipcRenderer;
ipcRenderer.on('data', function(evt, data) {
console.log(data); // {'a': 'b'}
});
See it from the documentation.
Upvotes: 1