Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question