如何在React Redux中呈现ListItem?

时间:2017-05-23 15:52:19

标签: reactjs redux react-redux

我是学习React + Redux的初学者。我正在努力构建我的第一个List / ListItem并遇到错误。这是我的组成部分:

import React from 'react';
import PropTypes from 'prop-types';
import {Link} from 'react-router';

const CatList = ({cats}) => {
  return (
    {cats.map(cat => (
      <li key={cat.id}>
        {cat.name}
      </li>
    ))}
  );
};

CatList.propTypes = {
  cats: PropTypes.array.isRequired
};

export default CatList;

我得到的错误:

Error in ./src/components/cats/CatList.js
Syntax error: Unexpected token, expected , (7:9)

   5 | const CatList = ({cats}) => {
   6 |   return (
>  7 |     {cats.map(cat => (
     |          ^
   8 |       <li key={cat.id}>
   9 |         {cat.name}
  10 |       </li>

Error
 @ ./src/components/cats/CatsPage.js 20:15-35

1 个答案:

答案 0 :(得分:2)

React组件应该返回单个元素。请尝试这个(只需将你的标记包装在aditional div中):

const CatList = ({cats}) => {
  return (
    <div>
       {cats.map(cat => (
         <li key={cat.id}>
           {cat.name}
         </li>
       ))}
    </div>
  );
};