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

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


import React from 'react';



class LightCounter extends React.Component{https://img1.sycdn.imooc.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 回答

timerTriker = () =>{

this.setState({

strikes:this.state.strikes+100

})

}

componentDidMount = () => {

setInterval(this.timerTriker,1000)

}


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

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

举报

0/150
提交
取消
React组件
  • 参与学习       26075    人
  • 解答问题       27    个

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

进入课程

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

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信