9951 explained code solutions for 126 technologies


reactjsHow can I use ReactJS and Electron together to develop a desktop application?


ReactJS and Electron can be used together to create powerful desktop applications. The process involves creating a ReactJS app, then using Electron to package it into a desktop application.

First, create a ReactJS app using the create-react-app command.

npx create-react-app my-app

This will create a ReactJS project in the my-app folder.

Next, install Electron in the project folder.

npm install electron --save-dev

Then create a main.js file in the project root folder. This is the entry point for the Electron application.

const { app, BrowserWindow } = require('electron')

let mainWindow

app.on('ready', () => {
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600
  })
  mainWindow.loadURL(`file://${__dirname}/build/index.html`)
})

Finally, add a script to the package.json file to start the Electron app.

"scripts": {
  "start": "electron ."
}

Now you can run npm start to launch the Electron application with the ReactJS app.

Helpful links

Edit this code on GitHub