[英]Component can't catch error from function passed as a prop
I have a Form component, and when the submit button is clicked, it calls: 我有一个Form组件,当单击Submit按钮时,它调用:
//Form.js
//...
const submit = () => {
try {
onSubmit(values); //onSubmit from props, values from state
} catch (error) {
console.log(error);
setSubmitError(error);
}
}
//...
I'm trying to use it with onSubmit
passed as: 我正在尝试通过以下方式与
onSubmit
一起使用它:
export const signIn = ({ email, password }) => (
auth()
.signInWithEmailAndPassword(email, password)
.catch(e => { throw e })
);
But it doesn't handle it. 但是它无法处理。 In the current version, it throws an error to the console:
auth.esm.js:429 Uncaught The email address is badly formatted.
在当前版本中,它向控制台抛出错误:
auth.esm.js:429 Uncaught The email address is badly formatted.
I tried wrapping the error with new Error
, but then it throws it to the window. 我尝试用
new Error
包装new Error
,但随后将其抛出到窗口中。 What am I doing wrong? 我究竟做错了什么?
A normal try/catch block cannot catch async errors. 正常的try / catch块无法捕获异步错误。 However you can declare an async block and this should work:
但是,您可以声明一个异步块,这应该可以工作:
const submit = async () => {
try {
await onSubmit(values); //onSubmit from props, values from state
} catch (error) {
console.log(error);
setSubmitError(error);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.