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>
)
}
}
2020-12-14
額,我也被坑了,另外state必需先初始化,不然我也一直報錯
2019-01-07
<Button onClick={this.click.bind(this)}>改變名字</Button>
這樣就可以了,應(yīng)該是要綁定this
2018-12-30
我也遇到這個問題 了
2018-12-27
ok,換到weapp 竟然可以了,吃驚!