0

Overview

In the snipped below I get a parameter to the pickFolder() called defaultPath. pickFolder is in the electron render process. It is doing a remote call to utils.openFolder (in the main process) with a callback (I have tried arrow callbacks as well as function callbacks).

Issue

In the example below, I thought that directly binding the defaultPath to the callback function would make sure the value would arrive in the callback. As you can see on the screen, the local context does not have defaultPath, but the enclosing closure does. But the real issue (as seen in the red text at the bottom in the console, is that defaultPath cannot be found, even though I am at that break-point).

Any suggestions?

enter image description here

1 Answer 1

1

This is what I found. That the callback that electron is doing for the remote call to open the dialog, is binding the "this" to the global state. So all the context in the closure when remote.ShowOpenDialog() was called is being lost.

The only way I have been able to capture those values, is to create a bind function and explicitly give a value to this, which contains the values I want to have available in the callback. This means I cannot use an Arrow function.

The result is shown below, and you can see that getState is now coming from this and the debugger confirms that this.defaultPath is defined.

export const pickFolderFun = (defaultPath) => (dispatch, getState) => {
    let next = function(folders) {
        if (folders == undefined) return
        let fieldName = this.getState().UX.fieldName
        FormChanger(fieldName, folders[0])
    }.bind({defaultPath, dispatch, getState})
    dialog.showOpenDialog({
        title: 'Select Source Folder',
        defaultPath: defaultPath,
        properties: ["openDirectory"]
    }, next)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.