Skip to content
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

Update templates to include an example of main -> renderer communication #32

Open
4 tasks done
faribauc opened this issue Mar 20, 2024 · 0 comments
Open
4 tasks done
Labels
enhancement New feature or request

Comments

@faribauc
Copy link

faribauc commented Mar 20, 2024

Clear and concise description of the problem

I'm having a hard time getting the renderer process (React app) listening to a main thread event. The project was created using the react-ts template.

I have an event being triggered from the main thread (every 10 seconds, for testing purposes):

  updater.autoUpdater.on('update-downloaded', (info) => {
    console.log('AutoUpdater::update-downloaded')
    mainWindow?.webContents.send('update-downloaded', info)
    setTimeout(() => updater.autoUpdater.checkForUpdatesAndNotify(), 1000 * delayBeforeNextCheck)
  })

and trying to subscribe to it from the App component:

import Versions from './components/Versions'
import electronLogo from './assets/electron.svg'
import { useEffect } from 'react'

function App(): JSX.Element {
  const ipcHandle = (): void => window.electron.ipcRenderer.send('ping')
  const updateApp = (): void => window.electron.ipcRenderer.send('update')

  const updateAvailable: string | null = null

  useEffect(() => {
    const callback = (...args: unknown[]): void => {
      console.log('args', args)
      // Set updateAvailable
    }

    const removeListener = window.electron.ipcRenderer.on('update-downloaded', callback)

    return () => {
      removeListener()
    }
  }, [])

  return (
    <div>{updateAvailable}</div>
  )
}

export default App

The event ls never fired in the React component.

Used Scaffolding

create-electron

Suggested solution

A simple example or replying to the "ping" by sending back "pong" to the renderer instead of just console logging it.

I don't have a solution for the moment as I can't get it to work.

Alternative

No response

Additional context

No response

Validations

@faribauc faribauc added the enhancement New feature or request label Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant