簡體   English   中英

如何在React.js中發現錯誤(“意外令牌”)?

[英]How to find error (“Unexpected token”) in React.js?

我一直在進步,突然間一切都崩潰了。 我在瀏覽器中收到以下錯誤消息:

./src/PokedexSelector.js
Syntax error: Unexpected token (48:9)

  46 |   
  47 | 
> 48 |   export default PokedexSelector;
     |          ^
  49 | 

這對我來說沒有任何意義,因為我嘗試更改代碼中的許多內容並查找缺少的花括號等,但無法確定問題的根源。 即使我更改了內容,錯誤也會四處跳動。 例如,如果刪除最后一行並刷新,則會得到以下信息:

./src/PokedexSelector.js
Syntax error: Unexpected token (46:0)

  44 |     )
  45 |   }
> 46 | 
     | ^

...對我來說毫無意義。

這是整個文件:

import React, { Component } from 'react';
import { capitalize } from './HelperFunctions';

class PokedexSelector extends Component {
  constructor(props) {
    super(props);
    this.state = {value: "National", pokedexes: []};

    this.handleChange = this.handleChange.bind(this);
    this.generatePokedexList = this.generatePokedexList.bind(this);
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  generatePokedexList() {
    const pokedexes = this.state.pokedexes;
    fetch("https://pokeapi.co/api/v2/pokedex/")
    .then(response => response.json())
    .then(myJson => {
      let results = myJson["results"];
      results.forEach(function(pokedex) {
        let pokedexName = pokedex["name"];
        let pokedexLink = "https://pokeapi.co/api/v2/pokedex/" + pokedexName;
        let pokedexDisplayName = capitalize(pokedexName.replace('-',' '));
        pokedexes.push(
          {
            name: pokedexName,
            "displayName": pokedexDisplayName,
            "link": pokedexLink
          }
        );
      });
    })
  }

  render() {
    this.generatePokedexList();
    return (
      <select id="pokedex-selector" value={this.state.value} onChange={this.handleChange()}>
        <option> {capitalize(this.state.value)} </option>
      </select>
    )
  }


  export default PokedexSelector;

渲染后添加“}”。 班仍然開放。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM