Setting Up React with Electron
Configure Electron with Vite and React, set up contextBridge for secure IPC, and run in development mode.
Setting Up React with Electron is a free React Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Project Setup Overview
Setting up React with Electron starts with creating a Vite React project, then adding electron and electron-builder as devDependencies. The project ends up with two entry points: the Vite app for the renderer and a main.js file for the Electron main process.
The main.js File
main.js is the Electron entry point. It creates a BrowserWindow with your desired window dimensions and security settings, then loads the React app — pointing to localhost:5173 during development and to a built file path in production.
electron-vite: The Recommended Toolkit
electron-vite is an all-in-one build tool that integrates Vite with Electron, handling the complex multi-process build configuration automatically. It provides separate Vite configs for main, preload, and renderer, and simplifies both development and production builds significantly.
Configuring vite.config.js
When configuring Vite for Electron, you must set explicit browser targets rather than relying on auto-detection, because the renderer runs in a specific Chromium version bundled with Electron. electron-vite handles this automatically, but manual setups require specifying the chrome version in the build target.
The Dev Workflow
Running electron-vite dev starts both the Vite development server for the renderer and the Electron main process simultaneously. The Vite dev server provides Hot Module Replacement, so changes to your React components appear instantly in the Electron window without a full restart.
Hot Module Replacement in Electron
HMR works in the Electron renderer just as it does in a browser — edited components re-render instantly while preserving state. Only changes to main.js or the preload script require a full Electron restart, since those run outside of the Vite HMR boundary.
TypeScript Configuration
Electron + React + TypeScript projects need separate tsconfig files for main (targeting Node.js/CommonJS), preload (targeting browser-like environment with Node globals), and renderer (standard browser TypeScript). electron-vite scaffolds these configurations automatically.
Setting Up contextBridge
The preload script uses contextBridge.exposeInMainWorld to expose your custom API to the renderer. A typical pattern is exposing a namespace like "api" with functions that wrap ipcRenderer.invoke calls, giving React components a clean async interface to main-process operations.
Accessing Exposed APIs from React
From any React component, you call window.api.myFunction() to invoke the exposed preload API. TypeScript users typically add a global declaration (window: Window & { api: MyAPI }) so they get type checking and autocompletion for the exposed interface.
Building for Production
electron-vite build compiles all three targets (main, preload, renderer) into the dist/ folder. electron-builder then packages this into a platform-specific installer. The production main.js loads the renderer from the bundled file path rather than the dev server URL.
Common Setup Pitfalls
The most frequent setup mistakes are: forgetting nodeIntegration:false and contextIsolation:true (creates security holes), loading the wrong URL in main.js (dev vs prod), and importing Electron modules in the renderer directly (causes crashes). electron-vite defaults prevent most of these issues.
electron-vite Dev Command
What does running electron-vite dev accomplish?
Lesson Recap
Setting up React with Electron is streamlined by electron-vite, which handles the multi-process build complexity. The dev workflow gives you full HMR support, contextBridge secures the renderer API surface, and separate TypeScript configurations ensure each process has the correct type environment. Production builds use electron-builder for platform-specific packaging.
Frequently asked questions
Is the “Setting Up React with Electron” lesson free?
Yes — the full text of “Setting Up React with Electron” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.
What will I learn in “Setting Up React with Electron”?
Configure Electron with Vite and React, set up contextBridge for secure IPC, and run in development mode. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start React Academy?
No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Setting Up React with Electron” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this React Academy lesson?
Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.