How do I properly fetch data with an IPC handler rom React in Electron?

Viewed 45

I have this code in my Electron window, which listens for a channel and a type of message, and returns various things based on the action.

useEffect(() => {
    getJeopardyCategories()
        .then(((categories) => {
            console.log(categories);
            setJeopardyCategories(categories)
        }))
        .catch(console.error);
}, []);

It calls a function, getJeopardyCategories that is defined as follows:

async function getJeopardyCategories(): Promise<(string | null)[]> {
    return await ipcRenderer.invoke(Channel.JEOPARDY, {
        action: JeopardyAction.GET_CATEGORIES,
    });
}

That IPC handler is defined on the electron side like follows

ipcMain.handle(Channel.JEOPARDY, async (event, data: JeopardyActionData) => {
    const j = globalData.gameFile.getJeopardyData();

    switch (data.action) {
        // ...
        case JeopardyAction.GET_CATEGORIES: {
            event.returnValue = j.categories;
            console.log(event.returnValue)
            break;
        }
        // ...

When I run my code, the electron logs show [ null, null, null, null ] which is the desired outcome for both sides. However when I look at the client logs it shows undefined.

0 Answers
Related