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

Missing semicolon in constructor (React + Spotify API)

我正在关注 youtube 教程,因此我可以将 spotify 的 api 与我的 React 应用程序一起使用。本教程是从 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;




I Believe this line is wrong, should be a class

function App() {

改为这样写:

class App extends React.Component{

如果还没有导入 react(文件顶部)

import React from 'react';