You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native Android开发语法错误求助

问题:React Native中inputChange方法提示缺失分号,代码运行报错

你的代码里存在多处语法错误,编辑器提示“缺失分号”其实是语法解析失败后的误报,修复以下问题即可解决:

  • 缺失Component导入:代码中使用class App extends Component但未导入Component,需要在顶部添加:

    import { Component } from 'react'
    
  • state属性名语法错误:state里的input value包含空格,属于无效对象属性名,改为与后续变量一致的inputValue:

    this.state = {
        inputValue: '',
        todos: [],
        type: 'All'
    }
    
  • 类方法定义位置错误:你把inputChange方法写在了constructor内部,不符合类的语法规范,需将方法移到constructor外部、类的顶层作用域:

    class App extends Component {
        constructor () {
            super()
            this.state = {
                inputValue: '',
                todos: [],
                type: 'All'
            }
            // 可选:如果后续调用this.inputChange时出现this指向问题,可在这里绑定this
            // this.inputChange = this.inputChange.bind(this)
        }
    
        inputChange (inputValue) {
            this.setState({inputValue})
        }
    
        render () {
            // ... 原有render代码
        }
    }
    
  • 组件props名错误:Input组件的input Value包含空格,改为inputValue:

    <Input
        inputValue={inputValue}
        inputChange={(text) => this.inputChange(text)} />
    

修复所有语法错误后,代码即可正常运行,“缺失分号”的提示也会随之消失。

内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 06:39:35