Electron Practical Projects | Beginner's Tutorial
\\n\\nThrough practical projects, you can deepen your understanding of various Electron features, including window management, IPC communication, file operations, data storage, and UI optimization.
\\n\\n\\n\\n
Project 1: Desktop Notes Application
\\n\\nFeature Design
\\n\\n- \\n
- Create, edit, and delete notes \\n
- Auto-save and local storage \\n
- Support for multiple windows editing simultaneously \\n
- Clean interface with Markdown rendering support \\n
Technology Stack
\\n\\n- \\n
- Frontend Framework: Vue or React \\n
- Renderer Process: Display note content, support rich text / Markdown \\n
- Data Storage:
electron-storefor saving note data \\n - Main Process: Manage windows, menus, and shortcuts \\n
- IPC Communication: Renderer process requests main process to read or save data \\n
Implementation Steps
\\n\\n1. Create Electron Project
\\n\\nmkdir electron-notes\\ncd electron-notes\\nnpm init -y\\nnpm install electron electron-store\\n\\n2. Main Process main.js
\\n\\nExample
\\n\\nconst { app, BrowserWindow, ipcMain } = require('electron')\\nconst Store = require('electron-store')\\n\\nconst store = new Store()\\nlet mainWindow\\n\\napp.whenReady().then(() => {\\n mainWindow = new BrowserWindow({\\n width: 800,\\n height: 600,\\n webPreferences: {\\n preload: `${__dirname}/preload.js`\\n }\\n })\\n mainWindow.loadFile('index.html')\\n})\\n\\n// IPC: Read notes\\nipcMain.handle('get-notes', () => store.get('notes', []))\\n\\n// IPC: Save notes\\nipcMain.handle('save-notes', (event, notes) => store.set('notes', notes))\\n\\n3. Preload Script preload.js
\\n\\nExample
\\n\\nconst { contextBridge, ipcRenderer } = require('electron')\\n\\ncontextBridge.exposeInMainWorld('api', {\\n getNotes: () => ipcRenderer.invoke('get-notes'),\\n saveNotes: (notes) => ipcRenderer.invoke('save-notes', notes)\\n})\\n\\n4. Renderer Process index.html
\\n\\nExample
\\n\\n<!DOCTYPE html>\\n<html>\\n<body>\\n <textarea id="note"></textarea>\\n <button id="saveBtn">Save</button>\\n <script>\\n const textarea = document.getElementById('note')\\n const btn = document.getElementById('saveBtn')\\n\\n window.api.getNotes().then(notes => {\\n textarea.value = notes.join('\\\\n\\\\n')\\n })\\n\\n btn.addEventListener('click', async () => {\\n await window.api.saveNotes([textarea.value])\\n alert('SaveSuccess')\\n })\\n </script>\\n</body>\\n</html>\\n\\nCode Analysis
\\n\\n- \\n
electron-store: Handles local data storage, automatically serializes JSON \\nipcMain.handle/ipcRenderer.invoke: Implements communication between renderer and main processes \\ncontextBridge: Securely exposes API to the renderer process \\ntextarea+button: Simple implementation for note content editing and saving \\n
\\n\\n
Project 2: File Manager
\\n\\nInterface Design
\\n\\n- \\n
- Directory tree on the left, file list on the right \\n
- Support file operations: create, delete, rename \\n
- Drag and drop files to target directories \\n
- Simple search and sort functionality \\n
File Operation Implementation
\\n\\n- \\n
- Node.js fs module: Read, write, delete files \\n
- path module: Path handling \\n
- IPC communication: Renderer process requests main process for file operations \\n
Example
\\n\\n// Main Process main.js\\nconst fs = require('fs')\\nconst path = require('path')\\n\\nipcMain.handle('read-dir', (event, dirPath) => fs.readdirSync(dirPath))\\nipcMain.handle('create-file', (event, filePath) => fs.writeFileSync(filePath, ''))\\nipcMain.handle('delete-file', (event, filePath) => fs.unlinkSync(filePath))\\n\\nExample
\\n\\n// Renderer Process\\nwindow.api.readDir('/Users/username/Documents').then(files => {\\n console.log(files)\\n})\\n\\nDrag and Drop Functionality
\\n\\n- \\n
- Renderer process listens for
dragstartanddropevents \\n - Main process uses
fs.renameorfs.copyFileto move files \\n
Example
\\n\\n// Renderer Process drop Event\\nconst dropArea = document.getElementById('fileList')\\n\\ndropArea.addEventListener('drop', (event) => {\\n event.preventDefault()\\n const filePath = event.dataTransfer.files.path\\n window.api.moveFile(filePath, targetDir)\\n})\\n\\nPerformance Optimization
\\n\\n- \\n
- Use asynchronous reading for large directories to avoid blocking the renderer process \\n
- Virtualize file lists to reduce DOM rendering pressure \\n
- Use batch updates or Web Workers for frequent operations \\n
\\n\\n
Summary
\\n\\nThrough these two practical projects, you can master:
\\n\\n- \\n
- Window and Multi-process Communication: Parent-child windows, IPC \\n
- Local Data Storage: electron-store, fs \\n
- UI and Performance Optimization: Virtual lists, lazy loading, asynchronous processing \\n
- Native Feature Integration: File operations, drag and drop, menus and shortcuts \\n
These projects can serve as foundational templates for subsequent complex desktop application development, helping you quickly build full-featured Electron applications.
YouTip