# Chapter 8 counter code not working

**URL:** <https://forum.kirupa.com/t/chapter-8-counter-code-not-working/644449>\
**Category:** web dev\
**Created:** [October 5, 2020, 10:33pm UTC](https://forum.kirupa.com/t/chapter-8-counter-code-not-working/644449 "2020-10-05T22:33:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yogendra\_Khopkar](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yogendra_khopkar/32/12585_2.png) [@Yogendra\_Khopkar](https://forum.kirupa.com/u/Yogendra_Khopkar)\
**Post date:** [October 5, 2020, 10:33pm UTC](https://forum.kirupa.com/t/chapter-8-counter-code-not-working/644449/1 "2020-10-05T22:33:20Z")

</div>

I am unable to get the code to display counter. I tried out the code and am getting error that “cannot read property strikes of undefined”

below is the source code

 React!React!React! #container{ padding:50px; background-color:#FFF; }

```
	</style>
</head>

<body>
	<div id="container"></div>
	<script type="text/babel">
	
	var destination = document.querySelector("#container");

	class LightningCounter extends React.Component{
		constructor(props, context){
			super(props, context);
			
			this.state = {
				strikes: 0
			};
		}
		
		timerTick() {
			this.setState({strikes: this.state.strikes + 100});
		}
		
		componentDidMount(){
			setInterval(this.timerTick,1000);
		}
		render(){
						
			return(<h1>{this.state.strikes}</h1>);
		}
	}
	
	class LightningCounterDisplay extends React.Component{
		render(){
			var divStyle={
			width:250,
			textAlign:"center",
			backgroundColor:"black",
			padding:40,
			fontFamily:"sans-serif",
			color:"#999",
			borderRadius:10
		};
		
			return(<div style={divStyle}><LightningCounter/></div>);
		}
	}
	
	
	
	ReactDOM.render(<div><LightningCounterDisplay /></div>,destination);
	
	
	</script>
</body>

```

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [October 5, 2020, 10:37pm UTC](https://forum.kirupa.com/t/chapter-8-counter-code-not-working/644449/2 "2020-10-05T22:37:41Z")

</div>

You’re losing context in your setTimeout. You can fix this by wrapping it in an arrow function

```auto
    setInterval(() => this.timerTick,1000);

```

This ensures timerTick is called off of `this` and not called as a standalone function reference where `this` becomes undefined (or `window` if not in strict mode).
