小记:把react项目从web迁移到electron

electron的启动模版:electron项目启动模版资源-CSDN文库

使用vite构建,包含了react react-router-dom tailwindcss mui 主副进程通讯

使用:

解压后使用vscode打开

终端里:npm install

然后npm run dev

打包:

windows系统计算机

npm run build:win

国产操作系统计算机

npm run build:linux

javascript 复制代码
src/main/index.js:

//创建窗口,ipc通讯
..............

  //更新单位名称
  ipcMain.on("updatecompany", async (event, arg) => {
    // console.log(arg);
    const settings = {
      method: "PUT",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json"
      },
      body: JSON.stringify({
        companyname: `${arg}`
      })
    };
    const res = await fetch(`http://192.168.1.99:7077/api/v1/store/company`, settings);
    const data = await res.json();
    event.returnValue = data;
  });

  //获取单位名称
  ipcMain.on("getcompany", async (event) => {
    const res = await fetch(`http://192.168.1.99:7077/api/v1/store/company`);
    const data = await res.json();
    event.returnValue = data;
  });

...............
javascript 复制代码
scr/preload/index.js

import { contextBridge } from "electron";
import { electronAPI } from "@electron-toolkit/preload";

// Custom APIs for renderer
const api = {};

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld("electron", electronAPI);
    contextBridge.exposeInMainWorld("api", api);
  } catch (error) {
    console.error(error);
  }
} else {
  window.electron = electronAPI;
  window.api = api;
}

src/renderer/scr/....:

company.jsx 示例文件

javascript 复制代码
...
useEffect(() => {
     //ipc通讯
   
     const result = window.electron.ipcRenderer.sendSync("getcompany");
     companyRef.current.value = result.companyname;
  }, []);
...

home.jsx 主页文件

main.jsx 启动文件

routemain.jsx 路由定义文件

相关推荐
LaughingZhu10 分钟前
Product Hunt 每日热榜 | 2026-05-21
前端·人工智能·经验分享·chatgpt·html
怕浪猫16 分钟前
Electron 开发实战(一):从零入门核心基础与环境搭建
前端·electron·ai编程
小鹏linux1 小时前
Ubuntu 22.04 部署开源免费具有精美现代web页面的Casdoor账号管理系统
linux·前端·ubuntu·开源·堡垒机
前端若水2 小时前
会话管理:创建、切换、删除对话历史
前端·人工智能·python·react.js
Bigger2 小时前
mini-cc:一个轻量级 AI 编程助手的诞生
前端·ai编程·claude
涵涵(互关)2 小时前
Naive-ui树型选择器只显示根节点
前端·ui·vue
BY组态2 小时前
Ricon组态系统最佳实践:从零开始构建物联网监控平台
前端·物联网·iot·web组态·组态
BY组态3 小时前
Ricon组态系统vs传统组态软件:为什么选择新一代Web组态平台
前端·物联网·iot·web组态·组态
SoaringHeart3 小时前
Flutter进阶:OverlayEntry 插入图层管理器 NOverlayZIndexManager
前端·flutter
放下华子我只抽RuiKe53 小时前
React 从入门到生产(四):自定义 Hook
前端·javascript·人工智能·深度学习·react.js·自然语言处理·前端框架