title: this.setState is not a function setInterval react
body: <p>I am trying to write a little snake game and having troubles with changing direction of the snake. I have included the whole component below.</p>
  
  <p>I am seeing the following error:</p>
  
  <p><code>this.setState is not a function</code></p>
  
  <p>This is being caused in the <code>changeDirection</code> method.</p>
  
  <p>See full code below:</p>
  
  <pre><code>export default class Example extends Component {
  
   constructor(props){
      super(props);
      this.state = {
        snakeLeftPos: 0,
        snakeDirection: 'right',
        boardWidth: 20,
        boardHeight: 20
      };
    }
  
   componentDidMount() {
     document.onkeydown = this.changeDirection;
     setInterval(() =&gt; {
        this.moveSnake();
     }, 1000);
   }
  
   moveSnake() {
      const { boardWidth, snakeDirection} = this.state;
  
      if(snakeDirection === 'right') {
        this.setState((prevState) =&gt; {
           return snakeDirection: prevState.snakeDirection + 20
        });
      }
  
      //same logic for other directions
    }
  
    changeDirection(e) {
      switch(e.keyCode) {
         case 37:
         this.setState(() =&gt; {
           return {
             snakeDirection: 'left'
           }
         });
         break;
  
         //other switch cases omitted for right, up, down
      }
    }
  
    render() {
  
      const { snakeLeftPos, boardHeight, boardWidth } = this.state;
      return(
        &lt;div&gt;
          &lt;Snake snakeLeftPos={snakeLeftPos} /&gt;
          &lt;Board boardHeight={boardHeight} boardWidth={boardWidth}/&gt;
        &lt;/div&gt;
      )
    }
  
  }
  </code></pre>