Home > Software design >  Missing semicolon in constructor (React Spotify API)
Missing semicolon in constructor (React Spotify API)

Time:11-19

I am following a youtube tutorial so I can use spotify's api with my react app. This tutorial is from November 2017 so maybe the syntax has changed. I copied the code exactly but receive a "missing semicolon." error. Can anyone help me? I have tried adding a semicolon in various places throughout the code but it doesn't fix the problem.

My code:

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;

Error message:

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;




CodePudding user response:

I Believe this line is wrong, should be a class

function App() {

write this instead :

class App extends React.Component{ {

also import react if not already done (top of the file)

import React from 'react';
  • Related