I am developing an app with Electron and React. Inside the app, I want to open a new window on buttonclick to show a tasklist (Tasklist.js is a component in my src directory). I am able to create and open a new child window with an onClick handler that uses ipcRenderer.send() to trigger the main.js to show the child window. However, I cannot load the page from my Tasklist.js component into the child window. If I do it directly, by inserting the path to Tasklist.js into childWindow.loadUrl(), the childWindow only renders my code inside the Tasklist.js. I have searched around quite a bit and understand that I need to have a DOM element to render the page. Is it somehow possible to render it from my root DOM tree without creating a new DOM element? E.g. something like childWindow.loadURL('index.html#/routeToTasklist')? I tried this (see exact code below) but I get the error “Cannot GET /index.html%23/tasklist”. Is there a way to modify the input into childWindow.loadURL() to make it work? Would very much appreciate any help!
See my main.js code below:
'use strict';
// Import parts of electron to use
const { app, Menu, BrowserWindow, ipcMain} = require('electron');
const path = require('path')
const url = require('url')
const Store = require('./src/components/shared/store/Store');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
let childWindow;
// Keep a reference for dev mode
let dev = false;
if (process.defaultApp || /[\/]electron-prebuilt[\/]/.test(process.execPath) || /[\/]electron[\/]/.test(process.execPath)) {
dev = true;
}
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 970,
height: 600,
show: false,
resizable: false,
backgroundColor: '#0C0456',
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
nativeWindowOpen: true
}
});
// and load the index.html of the app.
let indexPath;
if (dev && process.argv.indexOf('--noDevServer') === -1) {
indexPath = url.format({
protocol: 'http:',
host: 'localhost:8080',
pathname: 'index.html',
slashes: true
});
} else {
indexPath = url.format({
protocol: 'file:',
pathname: path.join(__dirname, 'dist', 'index.html'),
slashes: true
});
}
console.log(indexPath);
mainWindow.loadURL(indexPath);
//Remove menu
Menu.setApplicationMenu(null);
// Open the DevTools
mainWindow.webContents.openDevTools({"mode":"detach"});
// Don't show until we are ready and loaded
mainWindow.once('ready-to-show', () => {
mainWindow.show();
});
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
mainWindow.webContents.send('fromMain', "hello from main");
}
function openChildWindow(callback){
childWindow = new BrowserWindow({
width: 800,
height: 500,
parent:mainWindow,
show:true,
webPreferences:{
webSecurity: false,
nodeIntegration:true
}
});
let childPath;
if (dev && process.argv.indexOf('--noDevServer') === -1) {
childPath = url.format({
protocol: 'http:',
host: 'localhost:8080',
pathname: 'index.html#/tasklist',
slashes: true
});
} else {
childPath = url.format({
protocol: 'file:',
pathname: path.join(__dirname, 'dist', 'index.html#/tasklist'),
slashes: true
});
}
childWindow.loadURL(childPath);
childWindow.openDevTools();
childWindow.on('closed', function (){
childWindow=null;
});
}
ipcMain.on('openTasklist', event => {
if(!childWindow){
openChildWindow();
childWindow.show();
}else{
childWindow.show();
}
})
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', function (){
createWindow();
});
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
console.log("app is closed");
});
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});