Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

React + Electron Hands-on Project

React + Electron Hands-on Project

In this chapter we will use React + Electron to build a note-taking app. This hands-on project is named ReactElectronNotes, with features covering multiple windows, a tray, file operations and IPC communication, making it a very suitable project for beginners to practice.

If you are not yet familiar with React, you can first refer to:React Tutorial.

ReactElectronNotes The project will teach you to build, from scratch, a cross-platform desktop note-taking app, with the following features:

Project Structure Description

ReactElectronNotes/

│

├── src/                  # React 源码目录

├── build/                # 打包后的 React 静态文件

├── electron-main.js      # Electron 主进程

├── preload.js            # 渲染与主进程通信桥

├── note.html             # 子窗口页面

├── note.js               # 子窗口脚本

├── package.json

└── assets/

    └── icon.png          # 托盘图标


I. Project Feature Overview

Feature Module Description
Main window Display the list of all notes
child window New note input
Local storage Use electron-store Save data
Tray Minimize to tray; click to bring up the main window
IPC communication The renderer process interacts with the main process to read and write data

II. Project Initialization

1. Create a React project

npx create-react-app ReactElectronNotes

cd ReactElectronNotes

2. Install Electron and dependencies



npm install --save-dev electron electron-builder concurrently wait-on

npm install electron-store

3. Modify package.json

Ensure that scripts to add the Electron start command:

"main": "electron-main.js",

"scripts": {

  "react-start": "react-scripts start",

  "react-build": "react-scripts build",

  "electron-start": "concurrently \"npm run react-start\" \"wait-on http://localhost:3000 && electron .\"",

  "electron-build": "react-scripts build && electron-builder"

}


III. Create the Electron main process

New file electron-main.js:

Example

const

{

app

,

BrowserWindow

,

Tray

,

Menu

,

ipcMain

}

=

require

(

'electron'

)

const

path

=

require

(

'path'

)

const

Store

=

require

(

'electron-store'

)

let mainWindow

,

noteWindow

,

tray

const

store

=

new

Store

(

)

function

createMainWindow

(

)

{

  mainWindow

=

new

BrowserWindow

(

{

    width

:

800

,

    height

:

600

,

    webPreferences

:

{

      preload

:

path.

join

(

__dirname

,

'preload.js'

)

,

      contextIsolation

:

true

,

      nodeIntegration

:

false

   

}

 

}

)

 

if

(

process.

env

.

NODE_ENV

===

'development'

)

{

    mainWindow.

loadURL

(

'http://localhost:3000'

)

 

}

else

{

    mainWindow.

loadFile

(

path.

join

(

__dirname

,

'build/index.html'

)

)

 

}

  mainWindow.

on

(

'minimize'

,

(

e

)

=>

{

    e.

preventDefault

(

)

    mainWindow.

hide

(

)

 

}

)

  createTray

(

)

}

function

createNoteWindow

(

)

{

  noteWindow

=

new

BrowserWindow

(

{

    width

:

400

,

    height

:

300

,

    parent

:

mainWindow

,

    modal

:

true

,

    webPreferences

:

{

      preload

:

path.

join

(

__dirname

,

'preload.js'

)

,

      contextIsolation

:

true

,

      nodeIntegration

:

false

   

}

 

}

)

  noteWindow.

loadFile

(

path.

join

(

__dirname

,

'note.html'

)

)

}

function

createTray

(

)

{

  tray

=

new

Tray

(

path.

join

(

__dirname

,

'assets/icon.png'

)

)

 

const

menu

=

Menu.

buildFromTemplate

(

[

   

{

label

:

'Show Window'

,

click

:

(

)

=>

mainWindow.

show

(

)

}

,

   

{

label

:

'Exit'

,

click

:

(

)

=>

app.

quit

(

)

}

 

]

)

  tray.

setToolTip

(

'ReactElectronNotes'

)

  tray.

setContextMenu

(

menu

)

}

app.

whenReady

(

)

.

then

(

createMainWindow

)

ipcMain.

handle

(

'get-notes'

,

(

)

=>

{

 

return

store.

get

(

'notes'

,

[

]

)

}

)

ipcMain.

on

(

'save-note'

,

(

event

,

note

)

=>

{

 

const

notes

=

store.

get

(

'notes'

,

[

]

)

  notes.

push

(

note

)

  store.

set

(

'notes'

,

notes

)

  mainWindow.

webContents

.

send

(

'update-notes'

,

notes

)

}

)

app.

on

(

'window-all-closed'

,

(

)

=>

{

 

if

(

process.

platform

!==

'darwin'

)

app.

quit

(

)

}

)


IV. Create the Preload script

Create preload.js:

Example

const

{

contextBridge

,

ipcRenderer

}

=

require

(

'electron'

)

contextBridge.

exposeInMainWorld

(

'electronAPI'

,

{

  getNotes

:

(

)

=>

ipcRenderer.

invoke

(

'get-notes'

)

,

  saveNote

:

(

note

)

=>

ipcRenderer.

send

(

'save-note'

,

note

)

,

  onUpdateNotes

:

(

callback

)

=>

ipcRenderer.

on

(

'update-notes'

,

callback

)

}

)

Explanation:
Pass contextBridge to safely expose the API to the renderer process.


V. React front-end UI

src/App.js

Example

import

React

,

{

useState

,

useEffect

}

from

'react'

function

App

(

)

{

 

const

[

notes

,

setNotes

]

=

useState

(

[

]

)

  useEffect

(

(

)

=>

{

    window.

electronAPI

.

getNotes

(

)

.

then

(

setNotes

)

    window.

electronAPI

.

onUpdateNotes

(

(

_event

,

newNotes

)

=>

setNotes

(

newNotes

)

)

 

}

,

[

]

)

 

const

openNoteWindow

=

(

)

=>

{

    window.

open

(

'note.html'

,

'New Note'

,

'width=400,height=300'

)

 

}

 

return

(

   

<

div style

=

{

{

padding

:

20

}

}

     

<

h1

My Notes

h1

     

<

button onClick

=

{

openNoteWindow

}

New note

button

     

<

ul

{

notes.

map

(

(

n

,

i

)

=>

(

         

<

li key

=

{

i

}

{

n

}

li

       

)

)

}

     

ul

   

div

 

)

}

export

default

App

Create note.html The difference between note.js

note.html

Example

 

 

<p>New Note</p>

 

New note

 


 

 

note.js

Example

const

saveBtn

=

document.

getElementById

(

'saveBtn'

)

saveBtn.

addEventListener

(

'click'

,

(

)

=>

{

 

const

note

=

document.

getElementById

(

'note'

)

.

value

 

if

(

note.

trim

(

)

)

{

    window.

electronAPI

.

saveNote

(

note

)

    window.

close

(

)

 

}

}

)


VI. Run and Package

Start the development environment

npm run electron-start

After opening, the React main window appears; you can click "New Note" to pop up an input window.

Package and release

npm run electron-build

will generate cross-platform executable files (.exe, .app, .AppImage etc.).


VII. Extension Directions

Function Ideas
Markdown supports Use react-markdown Rendered content
Data search On the React side, via filter() implement local search
Auto-save Listen to the input box onChange Auto-save
Cloud sync Combine Firebase or a self-hosted REST API
Keyboard shortcut Use globalShortcut Register Ctrl+N to create a new note