-
Notifications
You must be signed in to change notification settings - Fork 97
How to use ipcRenderer and ipcMain? #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Sorry that the question is out of scope of this library... |
For anyone looking to use ipcRenderer with an Electron app built with TypeScript and React (following the directions in this repository's readme), I wanted to detail the steps I used. Create a import { contextBridge, ipcRenderer } from 'electron';
contextBridge.exposeInMainWorld('electron', {
doThing: () => ipcRenderer.invoke('do-thing')
}); Include that preload file in your import { app, BrowserWindow, ipcMain } from 'electron';
...
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
// Include the .js file, since it will be compiled from .ts by the time it is used by electron
preload: __dirname + '/preload.js'
}
});
...
ipcMain.handle('do-thing', (event, message) => {
console.log('Hello, World!');
}); With these steps, the Here is a sample export interface IElectronAPI {
doThing: () => void,
}
declare global {
interface Window {
electron: IElectronAPI
}
} Finally, you can use the import logo from './logo.svg';
import './App.css';
import Button from '@mui/material/Button';
function App() {
const doThing = () => {
window.electron.doThing();
}
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.tsx</code> and save to reload.
</p>
<Button onClick={doThing}>Click me</Button>
</header>
</div>
);
}
export default App; I hope this helps! I spent a lot of time off and on this past couple months trying to find a way to actually combine TypeScript, React, Electron, and Material UI and still be able to access the file system, so I am hoping to save time for others looking to do the same. This library had most of the moving pieces close to correct, but I had to do some digging to figure out the IPC stuff. |
@TeeGree, thanks for the helpful information. I'll reopen this issue, so that others can benefit from your comment. :) |
On my end, I didn't use preload.js to create exposedApi, instead I used the IpcRenderer directly in ReactApp.
then I can immediately use the ipc's .send() and .on() function in any of my react pages.
|
Can we use ipcRenderer in React App?
The text was updated successfully, but these errors were encountered: