Search code examples
electronipcipcrendereripcmain

using arg beyond ipcRenderer electron?


So I have this code and the arg is getting sent through from Main.

    ipcRenderer.send('asynchronous-message', 'async ping')

    ipcRenderer.on('asynchronous-reply', (event, arg) => {
      console.log(arg);
    });

I would like to be able to use the arg outside of the ipcRenderer function but I can't get it out. Is this even possible and if so then how? Thanks in advance.


Solution

  • You could set the value into an object property outside of the listener scope:

    const data = {
      arg: ''
    }
    
    ipcRenderer.on('asynchronous-reply', (event, arg) => {
      data.arg = arg
    })
    
    function someFunction (data) {
      if (data.arg === 'whatever') {
        ...
      }
    }
    

    When data.arg value changes, all the other functions it will get the updated value