reactjs - 映射数据的问题 - React
问题描述
你知道为什么我不能通过待办事项进行映射吗?我在网上关注教程,我和其他人完全一样,但它没有通过数据映射。我得到了 json,我可以在客户端控制台中看到待办事项。
import React, { Fragment, useEffect, useState } from "react";
const ListTodos = () => {
const [todos, setTodos] = useState([]);
const getTodos = async () => {
try {
const response = await fetch("http://localhost:5000/todos");
const jsonData = await response.json();
setTodos(jsonData);
} catch (err) {
console.error(err.message);
}
};
useEffect(() => {
getTodos();
}, []);
return (
<Fragment>
<table className="table mt-5 text-center">
<thead>
<tr>
<th>Description</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{todos.map((todo) => {
<tr>
<td>{todo.description}</td>
<td>Edit</td>
<td>Delete</td>
</tr>;
})}
</tbody>
</table>
</Fragment>
);
};
谢谢
解决方案
如果你在函数中使用花括号,你应该返回 JSX。像这样
{todos.map((todo) => {
return (<tr>
<td>{todo.description}</td>
<td>Edit</td>
<td>Delete</td>
</tr>;
)})}
否则用括号直接返回, Like This
{todos.map((todo) => (<tr>
<td>{todo.description}</td>
<td>Edit</td>
<td>Delete</td>
</tr>
))}
推荐阅读
- angular - 如何在角度中使用引导程序和 Ionic
- wpf - 如何按日期正确排序 ObservableCollection?
- sql - 如何将注释总和添加到原始模型的列
- javascript - 如何访问在组件内的自定义 Vue js 插件中声明的变量?
- reactjs - 类型“DefaultRootState”上不存在属性“y”
- python - 如何在 django 中仅获取当前用户的帖子
- c# - 当我在 Api 中调用日志或使用 Web Api 和 Xamarin 表单注册 Api 时出现错误:“Java.Net.SocketException”
- r - r 比较数据框的最大年/月与当前年月
- java - spring-security-oauth2 自定义错误处理
- javascript - 猫鼬异步等待卡住了