【发布时间】:2021-07-31 01:20:24
【问题描述】:
我的 React 文件中有以下钩子:
const [List, setList] = React.useState([]);
useEffect(() => {
axios
.get("http://localhost:8000/api/profile/")
.then((response) => {
setList(response.data);
console.log(response);
})
.catch((err) => console.log(err));
}, []);
它获取整个数据库的所有配置文件。然后我根据user.id显示具体用户的数据:
<ul>
<div>
{List.map((item, index) => {
if (
item.user_id === user.id // Line where I'm displaying data according to the user.id after fetching all data
) {
return (
<li key={item.id}>
<div>
{item.username}
</Link>
</div>
</li>
);
}
return null;
})}
</div>
</ul>
我的问题是,如何发送带有 user_id 的自定义 axios 请求,然后显示该用户的数据,而不是获取服务器的所有数据并对其进行过滤?
【问题讨论】:
-
很难说,因为我们不知道您正在使用的 API。路径、查询、正文参数?
-
@jonrsharpe 是 laravel 生成的 api。休息。
-
但这并没有告诉我们实际的界面是什么。一旦你发现,Axios 文档会显示一系列请求。
-
@jonrsharpe 对不起,我不明白你的问题。
-
什么是api get detail user infor?
标签: javascript php reactjs laravel axios