英文:
How to map array data into table row in React
问题
我只会为您翻译代码部分,不包括问题的回答。以下是代码的翻译部分:
import { useEffect, useState } from 'react';
import './App.css';
function Countries() {
const [data, setData] = useState([]);
useEffect(() => {
fetch('https://restcountries.com/v3.1/all?fields=name,continents,population,flag')
.then((resp) => resp.json())
.then((apiData) => {
setData(apiData);
});
});
return (
<div className="app-container">
<table>
<thead>
<tr>
<th>Name</th>
<th>Continent</th>
<th>Population</th>
<th>Flag</th>
</tr>
</thead>
<tbody>
{data.map((country) => (
<tr key={country.name}>
<td>{country.name}</td>
<td>{country.continents}</td>
<td>{country.population}</td>
<td>{country.flag}</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
export default Countries;
希望这可以帮助您的React项目从API获取数据并在表格中显示数据。
英文:
I'm just trying to implement a simple React project that retrieves data from an API and iterates over it to create table rows. I'm not getting any errors, but the data isn't appearing in the table.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
import { useEffect, useState } from 'react';
import './App.css';
function Countries() {
const [data, setData] = useState([]);
useEffect(() => {
fetch('https://restcountries.com/v3.1/all?fields=name,continents,population,flag')
.then((resp) => resp.json())
.then((apiData) => {
setData(apiData);
});
});
return (
<div className="app-container">
<table>
<thead>
<tr>
<th>Name</th>
<th>Continent</th>
<th>Population</th>
<th>Flag</th>
</tr>
</thead>
<tbody>
{data.map((country) => {
<tr>
<td>{country.name}</td>
<td>{country.continents}</td>
<td>{country.population}</td>
<td>{country.flag}</td>
</tr>
})}
</tbody>
</table>
</div>
);
}
export default Countries;
<!-- end snippet -->
Still there are no errors so I'm not sure why the data isn't appearing in the table.
Just returns a blank page.
Any help would be greatly appreciated.
答案1
得分: 1
你在tbody
标签内的map
函数中没有返回任何内容。你需要添加return
关键字或使用括号来包裹JSX。同时,将空数组作为useEffect
的第二个参数,以便只运行一次。
import { useEffect, useState } from 'react';
import './App.css';
function Countries() {
const [data, setData] = useState([]);
useEffect(() => {
fetch('https://restcountries.com/v3.1/all?fields=name,continents,population,flag')
.then((resp) => resp.json())
.then((apiData) => {
setData(apiData);
});
}, []); // 空数组在这里
return (
<div className="app-container">
<table>
<thead>
<tr>
<th>Name</th>
<th>Continent</th>
<th>Population</th>
<th>Flag</th>
</tr>
</thead>
<tbody>
{data.map((country) => (
<tr key={country.name}>
<td>{country.name}</td>
<td>{country.continents}</td>
<td>{country.population}</td>
<td>{country.flag}</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
export default Countries;
英文:
You're not returning anything from the map function inside the tbody
tag. You need to add the return
keyword or use parentheses to wrap the JSX. Also pass an empty array as the second argument to run useEffect
only once.
import { useEffect, useState } from 'react';
import './App.css';
function Countries() {
const [data, setData] = useState([]);
useEffect(() => {
fetch('https://restcountries.com/v3.1/all?fields=name,continents,population,flag')
.then((resp) => resp.json())
.then((apiData) => {
setData(apiData);
});
}, []); // empty array here
return (
<div className="app-container">
<table>
<thead>
<tr>
<th>Name</th>
<th>Continent</th>
<th>Population</th>
<th>Flag</th>
</tr>
</thead>
<tbody>
{data.map((country) => (
<tr>
<td>{country.name}</td>
<td>{country.continents}</td>
<td>{country.population}</td>
<td>{country.flag}</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
export default Countries;
答案2
得分: 1
以下是翻译好的部分:
你没有返回渲染的 `tr` 元素,因为你用花括号包裹它们而不是用圆括号。此外,`name` 是一个对象,你应该使用 `country.name.common`,而 `country.continents` 是一个数组,你应该将它连接成一个单独的字符串,或者迭代这些项目:
```jsx
{data.map(country => (
<tr key={country.flag}>
<td>{country.name.common}</td>
<td>{country.continents.join(', ')}</td>
<td>{country.population}</td>
<td>{country.flag}</td>
</tr>
))}
此外,你的 useEffect
没有依赖数组,这意味着它会在每个状态改变时运行(比如设置状态),这将导致另一个 API 调用和设置状态,从而导致无限循环。将一个空数组 []
设置为你的 useEffect
的依赖以防止这种情况发生。
示例:
const { useEffect, useState } = React;
function Countries() {
const [data, setData] = useState([]);
useEffect(() => {
fetch('https://restcountries.com/v3.1/all?fields=name,continents,population,flag')
.then(resp => resp.json())
.then(apiData => {
setData(apiData);
});
}, []);
return (
<div className="app-container">
<table>
<thead>
<tr>
<th>Name</th>
<th>Continent</th>
<th>Population</th>
<th>Flag</th>
</tr>
</thead>
<tbody>
{data.map(country => (
<tr key={country.flag}>
<td>{country.name.common}</td>
<td>{country.continents.join(', ')}</td>
<td>{country.population}</td>
<td>{country.flag}</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
ReactDOM
.createRoot(root)
.render(<Countries />);
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<div id="root"></div>
英文:
You are not returning the render tr
elements, because you wrapped them in curly brackets instead of parentheses. In addition, name
is an object, and you should use country.name.common
, and country.continents
is an array, and you should join it to a single string, or iterate the items:
{data.map(country => (
<tr key={country.flag}>
<td>{country.name.common}</td>
<td>{country.continents.join(', ')}</td>
<td>{country.population}</td>
<td>{country.flag}</td>
</tr>
))}
In addition, you the useEffect
doesn't have a dependencies array, which means that it would run on every state change (like setting the state), which would cause another api calls, and set state, which would lead to an infinite loop. Set an empty array []
as the dependencies of your useEffect
to prevent this.
Example:
<!-- begin snippet: js hide: true console: true babel: true -->
<!-- language: lang-js -->
const { useEffect, useState } = React;
function Countries() {
const [data, setData] = useState([]);
useEffect(() => {
fetch('https://restcountries.com/v3.1/all?fields=name,continents,population,flag')
.then((resp) => resp.json())
.then((apiData) => {
setData(apiData);
});
}, []);
return (
<div className="app-container">
<table>
<thead>
<tr>
<th>Name</th>
<th>Continent</th>
<th>Population</th>
<th>Flag</th>
</tr>
</thead>
<tbody>
{data.map(country => (
<tr key={country.flag}>
<td>{country.name.common}</td>
<td>{country.continents.join(', ')}</td>
<td>{country.population}</td>
<td>{country.flag}</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
ReactDOM
.createRoot(root)
.render(<Countries />);
<!-- language: lang-html -->
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<div id="root"></div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论