Nah
Nah

Reputation: 1768

How to integrate and run existing ReactJS Application in Electron

For instance, I have an ReactJS application: https://iaya-664f3.firebaseapp.com/ You can see in the HTML source the bundle.js file.

I have tried to run this application as desktop application using Electron, which should launch this web application in chromium window but it is not working.

Following is my main React application file app.js sitting in root directory. However compiled files bundle.js and index.html are in ./public/directory.

./app.js

import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, browserHistory } from 'react-router';
import routes from './routes';

import {Provider} from "react-redux";
import { createStore, applyMiddleware } from 'redux';
import ReduxPromise from 'redux-promise';
import rootReducer from './reducers/index';

const store = applyMiddleware(ReduxPromise)(createStore)(rootReducer);

ReactDOM.render(  <Provider store={store}>
                    <Router history={browserHistory}  routes={routes} />  
                </Provider> , 

                document.getElementById('react-app'));

./index.js

In this file I'm embedding my application to Electron to run in chromium.

var app = require("./app");
var BrowserWindow = require("browser-window");

// on electron has started up , booted up, everything loaded (Chromium ,goen, live)
app.on("ready", function(){
    var mainWindow = new BrowserWindow({
        width:800,
        height:600
    });
    mainWindow.loadUrl("file://" + __dirname+ "/public/index.html");
});

But this give some error on import React from 'React' line in my ./app.js.

So I assume that, I should only give ./public/index.html file to load which includes the compiled bundle.js. But I wonder, how that will work as the line app.on("ready", function(){ expect an app.

Moreover I have also tried following way in ./index.js but it gives some other error.

const electron = require('electron');

// Module to control application life.
const app = electron.app;

// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;

const path = require('path');
const url = require('url');

// 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;

function createWindow () {
    // Create the browser window.
    mainWindow = new BrowserWindow({width: 800, height: 600});

    // and load the index.html of the app.
    /*mainWindow.loadURL(url.format({
        pathname: path.join(__dirname, 'public/index.html'),
        protocol: 'file:',
        slashes: true
    }));*/
    mainWindow.loadURL("file://" + __dirname+ "/public/index.html");

    // Open the DevTools.
    mainWindow.webContents.openDevTools();

    // 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;
    });
}

app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', function () {
    // On OS X 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();
    }
});

app.on('activate', function () {
    // On OS X 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();
    }
});

Upvotes: 1

Views: 3230

Answers (1)

Nah
Nah

Reputation: 1768

Basically the thing is very simple. Electron acts just like a desktop chromium wrapper, that displays your any (any) type of web page inside desktop chromium.

So for example, we want to display http://www.google.com then you simply pass this URL to your loadURL() function.

Here is the working copy of code (asked in the question):

const electron = require('electron');
  // Module to control application life.
const app = electron.app;
  // Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;

app.on('ready', function(){
    var mainWindow = new BrowserWindow({width: 800, height: 600});

    mainWindow.loadURL("http://localhost:8080/");  // option1: (loading a local app running on a local server)

    //mainWindow.loadURL("https://iaya-664f3.firebaseapp.com");  // option2: (loading external hosted app)

    // loading developer tool for debugging
    mainWindow.webContents.openDevTools();
});

I hope this will clarify the confusion for many people, who are new to Electron. So the final words are that Electron only loads existing and running web application. It does not compile, does not act as a server. It is just a box in which you can put anything and give it a desktop sort of look e.g. menues, desktop notification, local file system access, etc.

Upvotes: 5

Related Questions