问答详情
源自:2-12 组件的状态机制(3)

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


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



提问者:qq_慕哥5037448 2019-05-22 17:48

个回答

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

    timerTriker = () =>{

    this.setState({

    strikes:this.state.strikes+100

    })

    }

    componentDidMount = () => {

    setInterval(this.timerTriker,1000)

    }


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