如何在React.js中发现错误(“意外令牌”)?

时间:2018-11-15 00:56:54

标签: reactjs error-handling syntax-error

我一直在进步,突然间一切都崩溃了。我在浏览器中收到以下错误消息:

./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;

1 个答案:

答案 0 :(得分:2)

在渲染后添加“}”。该课程仍然开放。