M
M
MariaM04792018-07-11 14:31:42
React
MariaM0479, 2018-07-11 14:31:42

How to fix Unexpected token, expected {?

Hello. I can't figure out why the console is throwing an error. Here is the code:

import React, {Component} from 'react'

class Timer extends Component({
  getInitialState: function(){
    return{
      seconds: 0
    };
},
    componentDidMount: function(){
      this.timer = setInterval(this.tick, 1000);
    },

    tick: function(){
      this.setState({seconds: this.state.seconds + 1});
    },

    componentWillUnmount: function(){
      clearInterval(this.timer);
    },

    render: function(){
      return(
        <h5>Прошло: {this.state.seconds}</h5>
      );
    }
});

export default Timer

5b45ea88836e0384395147.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anubis, 2018-07-11
@MariaM0479

In 20!8 the syntax is slightly different

import React, { Component } from 'react';

class Timer extends Component {
    state = {
        seconds: 0
    };
    
    componentDidMount() {
        this.timer = setInterval(this.tick, 1000);
    }

    tick() {
        this.setState({ seconds: this.state.seconds + 1 });
    }

    componentWillUnmount() {
        clearInterval(this.timer);
    }

    render() {
        return <h5>Прошло: {this.state.seconds}</h5>
    }
}

export default Timer;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question