简体   繁体   English

Windows.alert() 阻止访问/数据输入到电子反应应用程序中的文本输入字段。 处理 windows.alert 的最佳方法是什么?

[英]Windows.alert() preventing access/data entry into text input field in electron-react app. What is the best way to handle windows.alert?

I have a text input field in an electron-react app.我在电子反应应用程序中有一个文本输入字段。 The windows.alert() was being used to through an alert on altering a state given a certain condition. windows.alert() 被用来通过在给定条件下更改状态的警报。 But after throwing the alert, an input text field in a completely separate form would not allow the user to enter data without clicking out of the application and then back in.但是在抛出警报之后,一个完全独立的表单中的输入文本字段将不允许用户在没有点击应用程序然后再点击的情况下输入数据。

I do currently have a work around, which was throwing the alert using electron's ipcRenderer and ipcMain, but the style does not match well with the rest of the application.我目前有一个解决方法,它使用电子的 ipcRenderer 和 ipcMain 抛出警报,但该样式与应用程序的其余部分不太匹配。 Is there a way to handle windows.alert() that does not block data entry into text input fields?有没有办法处理 windows.alert() 不会阻止数据输入到文本输入字段? What causes windows.alert() to block entering data into text input?是什么导致 windows.alert() 阻止将数据输入到文本输入中?

Code Example Before:之前的代码示例:

function doSomething(value) {
    let array = otherArray;
    if (array.length == 0) {
        //The below was blocking typing into <input> in a completely separate form.
        window.alert("Please add expected value first")
    }
}

Work around code with ipcRenderer from preload.js and ipcMain:使用来自 preload.js 和 ipcMain 的 ipcRenderer 解决代码:

function doSomething(value) {
    let array = otherArray;
    if (array.length == 0) {
        window.api.send("send-alert", "Please add expected value first")
    }
}

//in main.js

ipcMain.on("send-alert", (event, incomingMessage)=> {
    let options = {
         message: incomingMessage
    }
    dialog.showMessageBox(mainBrowserWindow, options)
}

Use toastr to walk around this.使用 toastr 来解决这个问题。

Read this: https://github.com/CodeSeven/toastr阅读: https : //github.com/CodeSeven/toastr

Trigger an error or warning message in place of the window.alert() method.触发错误或警告消息代替 window.alert() 方法。 Ie: toastr.warning() or toastr.error()即: toastr.warning()toastr.error()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM