【问题标题】:Missing semicolon in constructor (React + Spotify API)构造函数中缺少分号(React + Spotify API)
【发布时间】:2021-12-29 19:07:52
【问题描述】:

我正在关注 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;




【问题讨论】:

    标签: reactjs api constructor spotify


    【解决方案1】:

    I Believe this line is wrong, should be a class

    function App() {
    

    改为这样写:

    class App extends React.Component{
    

    如果尚未完成,也导入 react(文件顶部)

    import React from 'react';
    

    【讨论】:

    • 谢谢!当我创建 react 应用程序时,没有 import React from 'react'; 我知道这是一个新的变化。
    • 另外function App() { 是默认值,以前是class App extends React.Component { {
    • 是的,但是当您使用功能组件时,没有构造函数,这就是您收到此错误的原因,没问题 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 2016-03-08
    • 2011-04-27
    相关资源
    最近更新 更多