最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React — Syntax error: Unexpected token, expected ; - Stack Overflow

programmeradmin14浏览0评论

For some reason that I can't figure it out I am getting a syntax error in the following React ponent. The error is in the first curly bracket on the renderItem(). What's wrong?

Thank you in advance.

import _ from 'lodash';
import React from 'react';
import { ToDoListHeader } from './todo-list-header';
import { ToDoListItem } from './todo-list-item';

export const ToDoList = (props) => {
  renderItems() {
    return _.map(this.props.todos, (todo, index) => <ToDoListItem key={index} {...todo} />)
  }

  return (
    <div>
      <table>
        <ToDoListHeader />
        <tbody>
          {/* {this.renderItems()} */}
        </tbody>
      </table>
    </div>
  );
}

For some reason that I can't figure it out I am getting a syntax error in the following React ponent. The error is in the first curly bracket on the renderItem(). What's wrong?

Thank you in advance.

import _ from 'lodash';
import React from 'react';
import { ToDoListHeader } from './todo-list-header';
import { ToDoListItem } from './todo-list-item';

export const ToDoList = (props) => {
  renderItems() {
    return _.map(this.props.todos, (todo, index) => <ToDoListItem key={index} {...todo} />)
  }

  return (
    <div>
      <table>
        <ToDoListHeader />
        <tbody>
          {/* {this.renderItems()} */}
        </tbody>
      </table>
    </div>
  );
}
Share Improve this question edited Jul 25, 2017 at 9:10 kind user 41.9k8 gold badges68 silver badges78 bronze badges asked Jul 24, 2017 at 11:33 Diego OrianiDiego Oriani 1,8975 gold badges23 silver badges36 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Well, you are getting error because you are defining the function like in a class, not in a function. Use a proper function declaration.

export const ToDoList = (props) => {
  const renderItems = () => {
    return _.map(this.props.todos, (todo, index) => <ToDoListItem key={index} {...todo} />)
  }

  return (
    <div>
      <table>
        <ToDoListHeader />
        <tbody>
          {/* {this.renderItems()} */}
        </tbody>
      </table>
    </div>
  );
}

It would work fine, if only ToDoList was a class, though.

  renderItems = () => {
    return _.map(this.props.todos, (todo, index) => <ToDoListItem key={index} {...todo} />)
  }

Your varian will work with es6 classes.

发布评论

评论列表(0)

  1. 暂无评论