简体   繁体   English

无法让 React 应用程序渲染,出现错误:未终止的 JSX 内容

[英]Can't get React app to render, getting error: Unterminated JSX contents

I'm trying to get data from an API in React using Axios.我正在尝试使用 Axios 从 React 中的 API 获取数据。 How do I get useEffect to work properly?如何让 useEffect 正常工作?

I am trying to build a page using data fetched from an API in React using Axios.我正在尝试使用 Axios 在 React 中使用从 API 获取的数据来构建页面。 I have a component called PersonList that should be fetching the data and sending it along to PersonCard, which then goes to my App.js.我有一个名为 PersonList 的组件,它应该获取数据并将其发送到 PersonCard,然后发送到我的 App.js。 I'm having trouble getting it to render, and getting a bunch of errors that I wasn't getting at first.我在渲染它时遇到了麻烦,并且遇到了一堆我一开始没有遇到的错误。 Here is my code in PersonList, which is the component that seems to be causing the error:这是我在 PersonList 中的代码,它似乎是导致错误的组件:

import React, { useEffect, useState } from 'react';
import axios from 'axios';
import PersonCard from '../components/PersonCard';
import { Container, Row } from 'reactstrap';

export default function PersonList ()
{

    const [people, setPeople] = useState([]);

    useEffect(() =>
    {
        axios
            .get(`https://swapi.co/api/people/1/`)
            .then(res =>
            {
                const personInfo = res.datas
                setPeople(personInfo)
                console.log('Star Wars Character Info:', personInfo)

            })
            .catch(err =>
            {
                console.log('The API data was not returned', err)
            });
    }, []);


    return (
        <Container>
            <Row>
          {people.map(person =>
            {
             <PersonCard
                name={person.name}
                height={person.height}
                weight={person.mass}
                hairColor={person.hair_color}
                birthday={person.birth_year}
                gender={person.gender}
                home={person.homeworld}
                species={person.species}
                films={person.films}
                vehicles={person.vehicles}
                ships={person.starships}
              /> 
          }
        )}
            </Row>
         </Container>

       )
    }

I should be able to console.log the data coming back from the API in the browser window, but not only can I not see the data, it's coming back undefined, but I am getting all kinds of errors includeing this one -我应该能够在浏览器 window 中对从 API 返回的数据进行控制台记录,但我不仅看不到数据,而且返回未定义,但我收到了包括这个在内的各种错误-

./src/components/PersonList.js
  Line 50:5:  Parsing error: Unexpected token, expected ","

  48 |             </Row>
  49 |         </Container>
> 50 |     };
     |     ^
  51 | }

You should remove return inside return, remove curly braces in people.map (see JS arrow function expressions ) and close every tag and braces you opened您应该删除 return 内的 return,删除people.map中的大括号(参见JS 箭头 function 表达式)并关闭您打开的每个标签和大括号

return (
  <Container>
    <Row>
      <Container>
        <Row>
          {people.map(person =>
            <PersonCard
              name={person.name}
              height={person.height}
              weight={person.mass}
              hairColor={person.hair_color}
              birthday={person.birth_year}
              gender={person.gender}
              home={person.homeworld}
              species={person.species}
              films={person.films}
              vehicles={person.vehicles}
              ships={person.starships}
            />
          )}
        </Row>
      </Container>
    </Row>
  </Container>
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM