Electron Hands-on Projects
Through hands-on projects, you can deepen your understanding of various Electron features, including window management, IPC communication, file operations, data storage, and UI optimization.
Project 1: Desktop note application
Feature design
- Create, edit, and delete notes
- Auto-save and local storage
- Support simultaneous editing in multiple windows
- Simple UI with Markdown rendering
Tech stack choices
- Front-end framework: Vue or React
- Renderer process: display note content, supporting rich text / Markdown
- Data storage:
electron-storeSave note data - Main process: manage windows, menus, and keyboard shortcuts
- IPC communication: the renderer process requests the main process to read or save data
Implementation Steps
1. Create an Electron project
mkdir electron-notes
cd electron-notes
npm init -y
npm install electron electron-store
2. Main process main.js
Example
const
{
app
,
BrowserWindow
,
ipcMain
}
=
require
(
'electron'
)
const
Store
=
require
(
'electron-store'
)
const
store
=
new
Store
(
)
let mainWindow
app.
whenReady
(
)
.
then
(
(
)
=>
{
mainWindow
=
new
BrowserWindow
(
{
width
:
800
,
height
:
600
,
webPreferences
:
{
preload
:
`$
{
__dirname
}
/
preload.
js
`
}
}
)
mainWindow.
loadFile
(
'index.html'
)
}
)
// IPC: Read notes
ipcMain.
handle
(
'get-notes'
,
(
)
=>
store.
get
(
'notes'
,
[
]
)
)
// IPC: Save notes
ipcMain.
handle
(
'save-notes'
,
(
event
,
notes
)
=>
store.
set
(
'notes'
,
notes
)
)
3. Preload script preload.js
Example
const
{
contextBridge
,
ipcRenderer
}
=
require
(
'electron'
)
contextBridge.
exposeInMainWorld
(
'api'
,
{
getNotes
:
(
)
=>
ipcRenderer.
invoke
(
'get-notes'
)
,
saveNotes
:
(
notes
)
=>
ipcRenderer.
invoke
(
'save-notes'
,
notes
)
}
)
4. Renderer process index.html
Example
Code Analysis
electron-store: responsible for local data storage, automatically serializing JSONipcMain.handle/ipcRenderer.invoke: implement communication between the renderer process and the main processcontextBridge: securely expose APIs to the renderer processtextarea+按钮: simply implement editing and saving of note content
Project 2: File manager
Interface design
- Directory tree on the left, file list on the right
- Support file operations: add, delete, rename
- Drag and drop files into the target directory
- Simple search and sort functions
File operation implementation
- Node.js fs module: read, write, and delete files
- path module: path handling
- IPC communication: the renderer process requests the main process to perform file operations
Example
// Main process main.js
const
fs
=
require
(
'fs'
)
const
path
=
require
(
'path'
)
ipcMain.
handle
(
'read-dir'
,
(
event
,
dirPath
)
=>
fs.
readdirSync
(
dirPath
)
)
ipcMain.
handle
(
'create-file'
,
(
event
,
filePath
)
=>
fs.
writeFileSync
(
filePath
,
''
)
)
ipcMain.
handle
(
'delete-file'
,
(
event
,
filePath
)
=>
fs.
unlinkSync
(
filePath
)
)
Example
// Renderer process
window.
api
.
readDir
(
'/Users/username/Documents'
)
.
then
(
files
=>
{
console.
log
(
files
)
}
)
Drag-and-drop functionality
- Renderer process listens
dragstartanddropEvents - Main process uses
fs.renameorfs.copyFileMove files
Example
// Renderer process drop event
const
dropArea
=
document.
getElementById
(
'fileList'
)
dropArea.
addEventListener
(
'drop'
,
(
event
)
=>
{
event.
preventDefault
(
)
const
filePath
=
event.
dataTransfer
.
files
[
0
]
.
path
window.
api
.
moveFile
(
filePath
,
targetDir
)
}
)
Performance optimization
- Use asynchronous reading for large directories to avoid blocking the renderer process
- Virtualize the file list to reduce DOM rendering pressure
- Use batch updates or a Web Worker for frequent operations
Summary
Through these two hands-on projects, you can master:
- Window and multi-process communication: parent-child windows, IPC
- Local data storage:electron-store, fs
- UI and performance optimization: virtual lists, lazy loading, asynchronous processing
- Native feature integration: file operations, drag-and-drop, menus, and keyboard shortcuts
These projects can serve as base templates for later complex desktop application development, helping to quickly build fully featured Electron apps.