YouTip LogoYouTip

Electron Practical

Electron Practical Projects | Beginner's Tutorial

\\n\\n

Through 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\\n

Feature 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
\\n\\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-store for 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
\\n\\n

Implementation Steps

\\n\\n

1. Create Electron Project

\\n\\n
mkdir electron-notes\\ncd electron-notes\\nnpm init -y\\nnpm install electron electron-store
\\n\\n

2. Main Process main.js

\\n\\n

Example

\\n\\n
const { 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\\n

3. Preload Script preload.js

\\n\\n

Example

\\n\\n
const { contextBridge, ipcRenderer } = require('electron')\\n\\ncontextBridge.exposeInMainWorld('api', {\\n  getNotes: () => ipcRenderer.invoke('get-notes'),\\n  saveNotes: (notes) => ipcRenderer.invoke('save-notes', notes)\\n})
\\n\\n

4. Renderer Process index.html

\\n\\n

Example

\\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\\n

Code Analysis

\\n\\n
    \\n
  • electron-store: Handles local data storage, automatically serializes JSON
  • \\n
  • ipcMain.handle / ipcRenderer.invoke: Implements communication between renderer and main processes
  • \\n
  • contextBridge: Securely exposes API to the renderer process
  • \\n
  • textarea + button: Simple implementation for note content editing and saving
  • \\n
\\n\\n
\\n\\n

Project 2: File Manager

\\n\\n

Interface 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
\\n\\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
\\n\\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\\n

Example

\\n\\n
// Renderer Process\\nwindow.api.readDir('/Users/username/Documents').then(files => {\\n  console.log(files)\\n})
\\n\\n

Drag and Drop Functionality

\\n\\n
    \\n
  • Renderer process listens for dragstart and drop events
  • \\n
  • Main process uses fs.rename or fs.copyFile to move files
  • \\n
\\n\\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\\n

Performance 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
\\n\\n

Summary

\\n\\n

Through 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
\\n\\n

These projects can serve as foundational templates for subsequent complex desktop application development, helping you quickly build full-featured Electron applications.

← Electron ApisElectron Electron Packaging An β†’