问答详情
源自:1-2 搭建Taro环境

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>

)

}

}




提问者:神秘的三三 2018-12-27 17:54

个回答

  • Lan_L
    2020-12-14 19:32:34

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

  • 慕神6482911
    2019-01-07 21:23:39

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

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

  • janenn
    2018-12-30 15:48:07

    我也遇到这个问题 了

  • 神秘的三三
    2018-12-27 17:57:39

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