Git Product home page Git Product logo

create-react-electron-app-example's Introduction

Introduction

This is an example repo of how to integrate a React app created using Create React App with Electron and includes an example of how to access Electron's IPC modules through the client-side React UI.

Credit goes to Bertrand Boustany for his great instructions here on how to easily integrate Electron with Create React App. .

However, in his instructions, he states this: "If your app needs to access Electron specific features such as IPC you will need to eject the react-scripts and set electron-renderer as your webpack target."

Credit goes to Brandon Bayer for his comment which explains how it is possible to access IPC modules without having to run eject on your create-react-app app .

Also, credit goes to Hamza Surti for his Medium article explaining what Electron's IPC modules are and an example of how to use them.

Last but not least, a great thanks to Christian Sepulveda for originally documenting the idea in this freeCodeCamp article.

Instructions

Initial Setup

Install Yarn if you don't already have it - instructions.

Initialize your project using create-react-app

npx create-react-app my-app
cd my-app
npm start

Integrating with Electron

Install dependencies for Electron

yarn add --dev concurrently electron-packager wait-on electron

Create a main.js file in the root of your repo and copy/paste the contents from this repo's main.js.

In your package.json, add these two lines in the root object to this:

  "homepage": "./",
  "main": "main.js",

The main property tells the electron command line script which file to run when it launches

create-react-app (by default) builds an index.html that uses absolute paths. This will fail when loading it in Electron. Thankfully, there is a config option to change this: set a homepage property in package.json. (source)

In your package.json, change your scripts object to this:

  "scripts": {
    "start": "concurrently 'yarn react-start' 'wait-on http://localhost:3000/ && yarn electron-start'",
    "build": "yarn react-build && yarn electron-build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "react-start": "BROWSER=none react-scripts start",
    "electron-start": "electron .",
    "react-build": "react-scripts build",
    "electron-build": "electron-packager ./ --overwrite"
  }

Feel free to reference this repo's package.json file if these instructions are unclear.

Updating your React app to use Electron's IPC modules

If you wish to update your app to be able to access IPC modules, refer to this repo's src/App.js for example code on how to do it.

The code you copied from this repo's main.js already includes code that will interact with the code in React. Feel free to rip it out of your app if you don't need it.

const ipcMain = require('electron').ipcMain;

// Electron IPC example
ipcMain.on('user-data', function(event, arg) {
  console.log(arg);
  //do child process or other data manipulation and name it manData
  event.sender.send('manipulatedData', 'COOL info!');
});

Building Your App

Your app is now ready to be built!

Run:

yarn build

and wait for the magic to happen. Refer to the console messages to see where the script put your newly built app.

Currently, the script is configured to build the application for your current OS, but you can add additional scripts to target different operating systems. Learn more here.

create-react-electron-app-example's People

Contributors

joeynguyen avatar

Stargazers

Willy N avatar Asep Bagja Priandana avatar Parthipan Natkunam avatar Rachael Arnold avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.