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
相关产品推荐
相关产品推荐

