繁体   English   中英

反应本机“ this.setState不是函数”

[英]React Native “this.setState is not a function”

我从本机反应开始,尝试将某些操作绑定到类方法,但是我遇到了一些关于未找到的方法的错误。

我尝试绑定:

class AccountsScreen extends Component {
  constructor (props) {
    super(props)
    this.userChange = this.userChange.bind(this)
    this.state = {
      user: '',
      password: ''
    }
  }
  render () {
    return (
      <View>
      <Text>Pass</Text>
      <TextInput
          onChange={this.userChange}
      />
      </View>
    )
  }
  userChange (user) {
    this.setState({user: user})
  }
}

和箭头功能

class AccountsScreen extends Component {
  constructor (props) {
    super(props)
    this.state = {
      user: '',
      password: ''
    }
  }
  render () {
    return (
      <View>
      <Text>Pass</Text>
      <TextInput
          onChange={(user) => this.userChange(user)}
      />
      </View>
    )
  }
  userChange (user) {
    this.setState({user: user})
  }
}

但我不断收到相同的错误:

“ this.setState不是函数”

完全卡住了。 有任何想法吗?

实际上,无需设置功能来设置状态,您可以执行此操作

<TextInput onChangeText={(user) => this.setState({user: user})} />

实际上,这是一个愚蠢的错误。 我使用的是onChange而不是onChangeText方法。

暂无
暂无

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

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