为了账号安全,请及时绑定邮箱和手机立即绑定

TypeError: Cannot read property 'setState' of undefined

import Taro, { Component } from '@tarojs/taro'

import { View, Text, Button } from '@tarojs/components'

import './index.less'


export default class Index extends Component {


config = {

navigationBarTitleText: '首页'

}

state = {

name:'里斯'

}


componentWillMount () { }


componentDidMount () {

// this.setState({name:'李四'})

}


componentWillUnmount () { }


componentDidShow () { }


componentDidHide () { }

click(){

this.setState({name:'章祠'});

}

render () {

return (

<View className='index'>

<Button onClick={this.click}>改变名字</Button>

<Text>{this.state.name}</Text>

</View>

)

}

}




正在回答

4 回答

额,我也被坑了,另外state必需先初始化,不然我也一直报错

0 回复 有任何疑惑可以回复我~

<Button onClick={this.click.bind(this)}>改变名字</Button>

这样就可以了,应该是要绑定this

0 回复 有任何疑惑可以回复我~

我也遇到这个问题 了

0 回复 有任何疑惑可以回复我~
#1

慕神6482911

<Button onClick={this.click.bind(this)}>改变名字</Button> 这样就可以了,应该是要绑定this
2019-01-07 回复 有任何疑惑可以回复我~

ok,换到weapp 竟然可以了,吃惊!

0 回复 有任何疑惑可以回复我~
#1

慕神6482911

<Button onClick={this.click.bind(this)}>改变名字</Button> 这样就可以了,应该是要绑定this
2019-01-07 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

TypeError: Cannot read property 'setState' of undefined

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号