React中Firebase登录代码报Parsing error: 缺少分号,求协助
解决React Firebase手机号登录中的"Missing semicolon"解析错误
先看修正后的可运行代码:
onSignInSubmit() { const phoneNumber = this.state.mobile; const appVerifier = window.recaptchaVerifier; signInWithPhoneNumber(auth, phoneNumber, appVerifier) .then((confirmationResult) => { // SMS sent. Prompt user to type the code from the message, then sign the // user in with confirmationResult.confirm(code). window.confirmationResult = confirmationResult; // ... }) .catch((error) => { // Error; SMS not sent // ... }); }
问题分析与解决:
- 核心错误:你代码里的
this.state.mobile()是错误写法。如果mobile是组件state中存储的手机号字符串(比如绑定自输入框的值),它是一个属性而非函数,不能加()调用。这个语法错误会让JS解析器无法正确识别代码结构,进而误报"缺少分号"的错误。 - 额外优化:链式调用的
.then和.catch换行并缩进,既提升可读性,也能避免部分解析器的格式敏感问题。 - 其他排查点:
- 确认
signInWithPhoneNumber和auth已正确导入到组件中,未导入的变量也可能导致解析器报错(有时语法错误的提示会有偏差)。 - 检查组件state定义,确保
mobile确实是字符串属性,而非函数。
- 确认
内容的提问来源于stack exchange,提问作者Ankit soni
相关产品推荐
相关产品推荐

