亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

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組件
  • 參與學習       26074    人
  • 解答問題       27    個

帶你了解如何創建好擁有各自狀態的組件,再由組件構成更加復雜的界面

進入課程

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

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號