构造函数(React+Spotify API)中缺少分号



我正在学习youtube教程,这样我就可以在我的react应用程序中使用spotify的api。本教程是从2017年11月开始的,所以语法可能发生了变化。我准确地复制了代码,但收到了一个";缺少分号"错误有人能帮我吗?我试着在整个代码的各个地方添加分号,但这并不能解决问题。

我的代码:

import './App.css';
function App() {
constructor(){
super();
const params = this.getHashParams();
const token = params.access_token;
if (token) {
spotifyApi.setAccessToken(token);
}
this.state = {
loggedIn: token ? true : false,
nowPlaying: { name: 'Not Checked', albumArt: '' }
}
};
getHashParams() {
var hashParams = {};
var e, r = /([^&;=]+)=?([^&;]*)/g,
q = window.location.hash.substring(1);
while ( e = r.exec(q)) {
hashParams[e[1]] = decodeURIComponent(e[2]);
}
return hashParams;
}
return (
<div className="App">
<a href='http://localhost:8888'>
<button>Login with Spotify</button>
</a>
<div> Now Playing: { this.state.nowPlaying.name } </div>
<div>
<img src={ this.state.nowPlaying.image } style={{ width: 100}}/>
</div>
</div>
);
}

export default App;

错误消息:

Failed to compile.
./src/App.js
SyntaxError: /Users/manuelfiestas/client/src/App.js: Missing semicolon. (4:15)
2 |
3 | function App() {
> 4 |   constructor(){
|                ^
5 |     super();
6 |     const params = this.getHashParams();
7 |     const token = params.access_token;


我认为这一行是错误的,应该是类

function App() {

改为写这个:

class App extends React.Component{

如果还没有完成,也导入反应(文件顶部(

import React from 'react';

最新更新