我需要帮助(React JS Hook中的API调用)为什么此北部有效?
我需要帮助(React JS Hook中的API调用)为什么此北部有效? 我需要从该API调用值
import React, { useEffect, useState } from 'react';
function Customers() {
const [customers, setCustomers] = useState(null);
useEffect(() => {
fetch('https://reactstarter-app.herokuapp.com/api/customers') **API CALLS**
.then(res => res.json())
.then(customers => setCustomers(customers))
}, [])
return (
<div>
<h2>Customers</h2>
<ul>
{customers.map((customer) => {
return <li key={customer.id}>{customer.firstName} {customer.lastName}</li>
})}
</ul>
</div>
);
}
export default Customers;
I need help (Api calls in React Js Hooks) Why is this nort working?
I need to call the values from that API
import React, { useEffect, useState } from 'react';
function Customers() {
const [customers, setCustomers] = useState(null);
useEffect(() => {
fetch('https://reactstarter-app.herokuapp.com/api/customers') **API CALLS**
.then(res => res.json())
.then(customers => setCustomers(customers))
}, [])
return (
<div>
<h2>Customers</h2>
<ul>
{customers.map((customer) => {
return <li key={customer.id}>{customer.firstName} {customer.lastName}</li>
})}
</ul>
</div>
);
}
export default Customers;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许这不是解决方案,但我无法粘贴代码发表评论,因此我必须发布一个答案:
Maybe it isn't a solution, but I cannot paste code to comment, so I have to post an answer:
看来您正在尝试通过零状态映射并可能遇到错误,使用条件渲染以避免错误并在API请求后渲染客户:
It looks like you're trying to map through a null state and probably getting an error, use conditional rendering to avoid the error and render the customers after the api request: