#Send Data from Main to Renderer through Preload

14 messages · Page 1 of 1 (latest)

prisma flicker
uneven plover
# prisma flicker https://www.electronjs.org/docs/latest/tutorial/ipc#pattern-3-main-to-renderer

I check out this but still unable to do this .
Here is my main.js
ipcMain.on('getMainValue', (event) => {
const mainValue = 'Hello from Main Value'
event.sender.send('mainValue', mainValue)
})
preload.js
contextBridge.exposeInMainWorld('EAPI', {
getMainValue: () => {
return new Promise((resolve, _reject) => {
ipcRenderer.send('getMainValue')
ipcRenderer.once('mainValue', (event, mainValue) => {
resolve(mainValue)
})
})
}
})

App.js
const [mainValue, setMainValue] = useState('')
useEffect(() => {
const getFromMain = async () => {
try {
const value = await window.EAPI.getMainValue()
setMainValue(value)
console.log(value)
} catch (error) {
console.log(error)
}
}
getFromMain()
}, [])

I got the value from main to rendere but not at the application load time. When I save my application second time I received the value, maybe some synchonization issues.

prisma flicker
#

Load time?

uneven plover
#

Yes

prisma flicker
#

What is it?

uneven plover
#

I mean when application run for the first time

prisma flicker
#

I don't see any code related to that

#

In your example

#

And use handle/invoke

#

If you need return value

#

Like request/response

uneven plover
#

Actually I don't need any return value, It should be one way data passing .

prisma flicker
#

Then i dont understand the issue

#

Does getMainValue work for you?