Michael
Michael

Reputation: 405

How do I remove a "window.api.receive" in Electron and React

I have an Electron app using the recommended setup from https://www.debugandrelease.com/the-ultimate-electron-guide/ where access is given via window.api.receive and window.api.send to the preloader. However I need to use window.api.receive within my react component and my issue is don't know how to remove the "receive" when the component is unmounted.

From the react side of things I think i just need to add in the code in the use effect, but I don't know what code can be used to remove the window.api.receive that was added.

  // On Mount
  useEffect(() => {
    console.log('mount APP')
    window.api.receive("helpLine", (text, loading) => {
      // Things are done here
  })
  }, []);

  // on Unmount
  useEffect( () => () => {
    console.log("unmount App")
    // window.api.receive should be removed here
  }, [] );

Upvotes: 2

Views: 802

Answers (1)

Kawnnor
Kawnnor

Reputation: 36

Use ipcRenderer.removeListener(channel, listener) to remove the specified listener.

https://www.electronjs.org/docs/latest/api/ipc-renderer#ipcrendererremovelistenerchannel-listener

Upvotes: 2

Related Questions