Unverified Commit 2cfcab8a authored by Henrik Wenz's avatar Henrik Wenz Committed by GitHub

Update with-electron example (#23873)

This PR updates `example/with-electron`:

- Updates electron to version [12 (latest stable)](https://www.electronjs.org/releases/stable#12.0.2)
- Introduces the new `contextBridge` to increase security ([docs](https://www.electronjs.org/docs/api/context-bridge))



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.

## Documentation / Examples

- [x] Make sure the linting passes
parent 2b48670b
const { ipcRenderer } = require('electron') const { ipcRenderer, contextBridge } = require('electron')
// Since we disabled nodeIntegration we can reintroduce contextBridge.exposeInMainWorld('electron', {
// needed node functionality here message: {
process.once('loaded', () => { send: (payload) => ipcRenderer.send('message', payload),
global.ipcRenderer = ipcRenderer on: (handler) => ipcRenderer.on('message', handler),
off: (handler) => ipcRenderer.off('message', handler),
},
}) })
...@@ -18,8 +18,8 @@ ...@@ -18,8 +18,8 @@
] ]
}, },
"devDependencies": { "devDependencies": {
"electron": "^7.1.7", "electron": "^12.0.2",
"electron-builder": "^21.2.0", "electron-builder": "^22.10.5",
"next": "latest", "next": "latest",
"react": "^16.12.0", "react": "^16.12.0",
"react-dom": "^16.12.0" "react-dom": "^16.12.0"
......
...@@ -6,16 +6,16 @@ const Home = () => { ...@@ -6,16 +6,16 @@ const Home = () => {
useEffect(() => { useEffect(() => {
const handleMessage = (event, message) => setMessage(message) const handleMessage = (event, message) => setMessage(message)
global.ipcRenderer.on('message', handleMessage) window.electron.message.on(handleMessage)
return () => { return () => {
global.ipcRenderer.removeListener('message', handleMessage) window.electron.message.off(handleMessage)
} }
}, []) }, [])
const handleSubmit = (event) => { const handleSubmit = (event) => {
event.preventDefault() event.preventDefault()
global.ipcRenderer.send('message', input) window.electron.message.send(input)
setMessage(null) setMessage(null)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment