TypeError: Cannot read property 'setState' of undefined

来源:1-2 搭建Taro环境

神秘的三三

2018-12-27 17:54

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回答

  • 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

    我也遇到这个问题 了

    慕神6482...

    <Button onClick={this.click.bind(this)}>改变名字</Button> 这样就可以了,应该是要绑定this

    2019-01-07 21:23:59

    共 1 条回复 >

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

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

    慕神6482...

    <Button onClick={this.click.bind(this)}>改变名字</Button> 这样就可以了,应该是要绑定this

    2019-01-07 21:23:51

    共 1 条回复 >

Taro多端框架开发外卖首页

Taro 一套代码多端运行的框架,带你掌握Taro开发基本语法,以及项目开发

22910 学习 · 82 问题

查看课程

相似问题