# In Chapter 7 react state this.strikes is undefined

**URL:** <https://forum.kirupa.com/t/in-chapter-7-react-state-this-strikes-is-undefined/652365>\
**Category:** web dev\
**Created:** [June 23, 2022, 6:26pm UTC](https://forum.kirupa.com/t/in-chapter-7-react-state-this-strikes-is-undefined/652365 "2022-06-23T18:26:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniil.ustimencko](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/daniil.ustimencko/32/18070_2.png) [@daniil.ustimencko](https://forum.kirupa.com/u/daniil.ustimencko)\
**Post date:** [June 23, 2022, 6:26pm UTC](https://forum.kirupa.com/t/in-chapter-7-react-state-this-strikes-is-undefined/652365/1 "2022-06-23T18:26:38Z")

</div>

When I run the following code from chapter 7

```auto
 class LightningCounter extends React.Component {
        constructor(props) {
          super(props);
          this.state = {
            strikes: 0,
          };
        }

        timerTick() {
          console.log(this)
          this.setState({
            strikes: this.state.strikes + 100,
          });
        }
        componentDidMount() {
            
            setInterval(this.timerTick, 2000);
        }

        render() {
          return <h1>{this.state.strikes}</h1>;
        }
      }

```

There happens an error, because _this_ in setInterval becomes window context, however it it just runs the function without setInterval it works correctly.

---

<div class="post-metadata">

**Author:** ![daniil.ustimencko](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/daniil.ustimencko/32/18070_2.png) [@daniil.ustimencko](https://forum.kirupa.com/u/daniil.ustimencko)\
**Post date:** [June 23, 2022, 6:39pm UTC](https://forum.kirupa.com/t/in-chapter-7-react-state-this-strikes-is-undefined/652365/2 "2022-06-23T18:39:46Z")

</div>

```auto
this.state = {
strikes: 0
};
this.timerTick = this.timerTick.bind(this)

```

This solves the problem (the solution provided later in the chapter), however, why list code that doesn’t work? Or it used to work before? Thanks.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [June 24, 2022, 3:03pm UTC](https://forum.kirupa.com/t/in-chapter-7-react-state-this-strikes-is-undefined/652365/4 "2022-06-24T15:03:46Z")

</div>

Hi @daniil.ustimencko - what edition of the book are you in? I looked at the content in the 2nd edition, and I did this deliberately to highlight the need to use `bind` a few paragraphs later. I may revisit this approach in a future edition where I make the code work from the beginning and call out the `bind` angle.
