Logo

dev-resources.site

for different kinds of informations.

First thoughts on Electron

Published at
1/8/2025
Categories
electron
Author
sarkispeha
Categories
1 categories in total
electron
open
Author
10 person written this
sarkispeha
open
First thoughts on Electron

As a web developer, I find that whenever I'm looking to try something new, I first see if there's a way that I can leverage my knowledge foundation to find similarities play into the learning experience. Electron allows for such a cross and some wonderful analogies to getting started with desktop application development.

State management

The overlap in using a store between React and Electron is a nice getter/setter model similar to most state management systems in React. By requiring the electron-store, one can easily get going:

// electron main.js
const Store = require('electron-store');

const store = new Store({
defaults: {
  groceryList: {
    produce: ["apples", "carrots", "pears"]
    }
  }
});

ipcMain.handle('get-produce', () => {
  return store.get('groceryList.produce');
});
Enter fullscreen mode Exit fullscreen mode
// ui.jsx (React component)
function ProduceListComponent() {
  const [produce, setProduce] = useState(null);

  useEffect(() => {
    window.electron.getProduce().then(setProduce);
  }, []);

// ... use 'produce' as needed
Enter fullscreen mode Exit fullscreen mode

The IPC as a REST API

Poking around at the tutorials, each is quick to get talking about the IPC. The IPC (Inter-Process Communication) is similar to an internal API within a web app. Where fetch is the browser's way to make a request, electron.ipcRenderer.invoke is how this would be exposed to the Electron app's UI. As can be seen, both are async and promise-based.

// Web API Request
const fetchPriceData = async () => {
  const response = await fetch('/api/price');
  return response.json();
};

// Electron IPC
const getPriceData = async () => {
  return await window.electron.invoke('get-price');
};
Enter fullscreen mode Exit fullscreen mode

Route Architecture

Similar to Node/Express, building a route for a web API is the equivalent to an IPC handler. HTTP methods are similar to the IPC channel names, and the request/response cycle is to the IPC invoke/return.

const express = require('express');
const app = express();

app.get('/api/price/:id', async (req, res) => {
  try {
    const productId = req.params.id;
    const productData = await database.getPriceById(productId);
    res.json(productData);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});
Enter fullscreen mode Exit fullscreen mode
const { ipcMain } = require('electron');

ipcMain.handle('get-product', async (event, productId) => {
  try {
    const productData = await database.getProductById(productId);
    return productData;
  } catch (error) {
    throw error;
  }
});
Enter fullscreen mode Exit fullscreen mode

It's been a fun start with Electron, and I'm looking forward to tinkering around its extensive API for building desktop applications.

electron Article's
30 articles in total
Favicon
First thoughts on Electron
Favicon
Let's build website builder
Favicon
Study With Me 1.0
Favicon
[Boost]
Favicon
Electric Bus Pantograph Market: Trends, Challenges, Drivers, and Insights Through 2033
Favicon
Keyboard Sounds β€” Make any keyboard sound mechanical
Favicon
Electron
Favicon
I Hate Website Builders – So I Built My Own
Favicon
Is the browser always the right tool for the job?
Favicon
E-House Market Insights: Compact Solutions for Modern Power Challenges
Favicon
.NET Cross-Platform Web Desktop App Frameworks as Electron Alternatives
Favicon
How to remotely EV code-sign a windows application using ssl.com
Favicon
Configuring webpack to handle multiple browser windows in Electron
Favicon
Using native modules in Electron
Favicon
Requesting camera and microphone permission in an Electron app
Favicon
πŸš€Building a Multi-Step Loading Screen with Electron
Favicon
Building deep-links in Electron application
Favicon
MaweJS: Editor for plantsers
Favicon
Handling TypeORM migrations in Electron apps
Favicon
Unicode-Search - my first Electron app!
Favicon
Creating a synchronized store between main and renderer process in Electron
Favicon
The ultimate Electron app with Next.js and React Server Components
Favicon
Electric Bikes And Coding
Favicon
Creating a Browser Window in Electron: A Step-by-Step Guide
Favicon
How to Create a Windows Executable with Electron Forge that Adds a Desktop Shortcut?
Favicon
Building and publishing an Electron application using electron-builder
Favicon
Cross-compile a distributed Electron App
Favicon
The Only Electron Framework You'll Ever Need: Introducing the Ideal Electron Framework
Favicon
Overcoming Electron-Builder Limitations: A C# and NSIS Hybrid Approach
Favicon
How to Use Electron.js to Create Cross-Platform Desktop Applications

Featured ones: