sinio
sinio

Reputation: 711

Electron: Using mainWindow.webContents.send when mainWindow loads

When a user changes the theme, I use mainWindow.webContents.send to change a class in the DOM. I also save it in the store, under the key theme.

mainWindow.webContents.send('theme:change', theme);
store.set('theme', theme);

Then in renderer.js:

ipcRenderer.on('theme:change', (event, theme) => {
  document.querySelector('body').className = `${theme}`;
});

This successfully changes the theme and saves it in the store. However, now I want that theme to load up when starting the application rather than going back to the default. To do this, in app.on('ready') I am doing this:

mainWindow.webContents.send('theme:change', store.get('theme'));

However, nothing is happening. It's like it isn't being sent. Where am I going wrong? Essentially what needs to be done is for the class in body to be changed when the application loads to the one in the store.

Upvotes: 2

Views: 1379

Answers (1)

sinio
sinio

Reputation: 711

Figured it out. I had to put:

mainWindow.webContents.once('dom-ready', () => {
    mainWindow.webContents.send('theme:change', store.get('theme'));
})

I was trying mainWindow.on('dom-ready') which is why it wasn't working.

Upvotes: 4

Related Questions