reactjs - 反应钩子和 fetch() 建议
问题描述
我有一个组件可以获取一个随机人,将其保存到一个状态变量数组中,并在通过该数组进行映射后,它返回某些元素。我的目标是使用头像和个人信息呈现“社交卡”,我想为此使用材质 UI CARD 组件。另外,我想在这里获得有关我的逻辑的任何反馈,这是否是实现我所取得的成就的好方法。setTimeout() 只是在那里,所以我可以渲染一些加载动画。此外,当我尝试在数组映射中返回完整的卡片组件时,我无法渲染 {item.something.something}
export default function SocialCardsfunction() {
const [items, setItems] = useState([]);
const [loading, setLoading] = useState(true);
const classes = useStyles();
const nextPerson = () => {
fetch("https://randomuser.me/api")
.then((response) => response.json())
.then((response) => {
setItems(response.results);
});
};
useEffect(() => {
fetch("https://randomuser.me/api")
.then((response) => response.json())
.then((response) => {
setTimeout(() => {
setItems(response.results);
setLoading(false);
}, 1000);
});
}, []);
if (loading) {
return <div>Loading ...</div>;
} else {
return (
<div>
{items.map((item, i) => {
return (
<div>
<h2>{item.name.first}</h2>
<img src={item.picture.large} alt='picture' key={i} />
<button onClick={() => nextPerson()}>click me</button>
</div>
);
})}
</div>
);
}
}
const useStyles = makeStyles({
root: {
minWidth: 275,
},
bullet: {
display: "inline-block",
margin: "0 2px",
transform: "scale(0.8)",
},
title: {
fontSize: 14,
},
pos: {
marginBottom: 12,
},
});
解决方案
所做的更改
- 不要重复相同的代码,而是创建一个函数并在
useEffect
. - 列表中的每个孩子都应该有一个唯一的 "
key
" 道具。 - 使用 Material UI 中的卡片。(我没有过多关注样式 XD)
import {
Button,
Card,
CardActions,
makeStyles,
} from "@material-ui/core";
import React, { useEffect, useState } from "react";
export default function SocialCardsfunction() {
const [items, setItems] = useState([]);
const [loading, setLoading] = useState(true);
const classes = useStyles();
const fetchPerson = () => {
fetch("https://randomuser.me/api")
.then((response) => response.json())
.then((response) => {
setTimeout(() => {
setItems(response.results);
setLoading(false);
}, 1000);
});
};
useEffect(() => {
fetchPerson();
}, []);
if (loading) {
return <div>Loading ...</div>;
} else {
return (
<div>
{items.map((item, i) => {
return (
<div key={i}>
<Card className={classes.root}>
<h2>{item.name.first}</h2>
<img
alt="img"
src={item.picture.large}
className={classes.large}
/>
<CardActions>
<Button onClick={() => fetchPerson()} size="small">Next</Button>
</CardActions>
</Card>
</div>
);
})}
</div>
);
}
}
const useStyles = makeStyles({
root: {
minWidth: 275
},
bullet: {
display: "inline-block",
margin: "0 2px",
transform: "scale(0.8)"
},
title: {
fontSize: 14
},
pos: {
marginBottom: 12
}
});
推荐阅读
- python - 如果我不想在使用函数后输出“无”,我该怎么办
- php - 在laravel的一个页面中制作2个搜索表单:一个在同一页面中,另一个在主页中
- python - 如何在关闭笔记本电脑的 GPU 服务器上训练深度学习模型?
- elasticsearch - 弹性搜索 DSL 与 SQL 性能
- python - TensorFlow 基础文本分类练习
- json - 如何通过匹配 Flutter 中的 JSON 响应来获取特定的字段名称?
- c# - 该进程无法访问文件“C:\Users\Abyss\Desktop\Test\filename.txt”,因为它正被另一个进程使用
- codeigniter - 使用 Codeigniter 在 Datatable 插件中实现自定义过滤器
- javascript - JS(或浏览器)改变了我文件的编码,如何解决这个问题?
- multi-factor-authentication - 注册后,MFA 电话号码不会出现在令牌声明中