react-native - can't access setState inside component function

Viewed 2721

I'm learning react-native and now I'm stucked on state lesson with this error:

_this2.setState is not a function.

Here's the current block of code.

...
export default class StopWatch extends Component {

  constructor(props){
    super(props);
    this.state = {
      timeElapsed: null
    }
  }

  handleStartStopClick(){
    var startTime = new Date();

    setInterval(() => {
      this.setState(previousState => {
        return {timeElapsed:new Date() - startTime};
      });
    }, 100);
  }
...

What am I doing wrong?

2 Answers
Related