Uncaught TypeError: Cannot read property 'strikes' of undefined at timerTriker (APP3.js:19)

来源:2-12 组件的状态机制(3)

qq_慕哥5037448

2019-05-22 17:48


import React from 'react';



class LightCounter extends React.Component{https://img1.mukewang.com/5ce51aea0001238816030980.jpg

constructor(props){

super(props);

this.state={

strikes:0

}

}

getInitialState(){

return {

strikes:0

}

}


timerTriker(){

this.setState({

strikes:this.state.strikes+100

})

}

componentDidMount(){

setInterval(this.timerTriker,1000)

}


render(){

return (

<div>

<h1>{this.state.strikes}</h1>

</div>

)

}

}



class LightCounterDisplay extends React.Component{

render(){

var divStyle ={

width:250,

textAlign:"center",

backgroundColor:"black",

padding:40,

color:"#999",

fontFamily:"sans-serif",

borderRadius:10

}

return (

<div style= {divStyle}>

<LightCounter />

</div>

)

}

}


export default LightCounterDisplay



写回答 关注

1回答

  • qq_慕哥5037448
    2019-05-22 18:05:00

    timerTriker = () =>{

    this.setState({

    strikes:this.state.strikes+100

    })

    }

    componentDidMount = () => {

    setInterval(this.timerTriker,1000)

    }


    解决了 没有绑定 这2个方法这样写 就可以了

React组件

带你了解如何创建好拥有各自状态的组件,再由组件构成更加复杂的界面

26078 学习 · 27 问题

查看课程

相似问题