Reputation: 636
In my component class I have created a constructor but its throwing error:
Component class with constructor:
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
constructor(props) {
super(props);
this.state = {
temp: 0,
desc: '',
icon: '',
loading: true
}
}
the error I am seeing :
./src/App.js
Line 5: Parsing error: Unexpected token, expected ";"
3 | import './App.css';
4 |
> 5 | constructor(props) {
| ^
6 | super(props);
7 | this.state = {
8 | temp: 0,
Can someone please advise if I am missing something.
Upvotes: 2
Views: 2507
Reputation: 41
You should use class based components, like:
import React from 'react';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
temp: 0,
.
.
.
}
}
}
Upvotes: 0
Reputation:
There is no class. It's just a function. Try this.
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class YourComponent extends Component {
constructor(props) {
super(props);
this.state = {
temp: 0,
desc: '',
icon: '',
loading: true
}
}
}
export default YourComponent;
Upvotes: 2
Reputation: 566
I'm not familiar with React, but from what I know of ES6, constructor
is only valid within a class
definition.
Upvotes: 3