首页 > 解决方案 > 我正在使用 React 创建一个表单并收到此错误

问题描述

这是我得到的错误:

./src/App.js
  Line 26:10:  Parsing error: Unexpected token, expected ";"

  24 |   }
  25 | }
> 26 | render() {
     |          ^
  27 |   const { characters } = this.state;
  28 | 
  29 |   return (
import React, { Component } from 'react'
import Table from './Table'

class App extends Component {
  state = {
    characters: [
      {
        name: 'Charlie',
        job: 'Janitor',
      },
      {
        name: 'Mac',
        job: 'Bouncer',
      },
      {
        name: 'Dee',
        job: 'Aspring actress',
      },
      {
        name: 'Dennis',
        job: 'Bartender',
      },
    ],
  }
}
render() {
  const { characters } = this.state;

  return (
    <div className="container">
      <Table characterData={characters} removeCharacter={this.removeCharacter} />
    </div>
  )
}

export default App

标签: node.jsreactjsreact-native

解决方案


您正在添加大括号

“{”

在错误的地方

让它像这样:

import React, { Component } from 'react'
import Table from './Table'

class App extends Component {
  state = {
    characters: [
      {
        name: 'Charlie',
        job: 'Janitor',
      },
      {
        name: 'Mac',
        job: 'Bouncer',
      },
      {
        name: 'Dee',
        job: 'Aspring actress',
      },
      {
        name: 'Dennis',
        job: 'Bartender',
      },
    ],
  }
render() {
  const { characters } = this.state;

  return (
    <div className="container">
      <Table characterData={characters} removeCharacter={this.removeCharacter} />
    </div>
  )
}
}
export default App;

基本上你的渲染函数在你的 App 类之外,所以这就是你的代码不起作用的原因。


推荐阅读