【发布时间】:2022-11-03 15:33:56
【问题描述】:
我的一个带有布尔值的 api 响应(名称:used),我的逻辑是如果使用响应将显示red_light 和green_light,如果不使用。
const red_light = <div className="h-2.5 w-2.5 rounded-full bg-red-700 mr-2"></div>
const green_light = <div className="h-2.5 w-2.5 rounded-full bg-green-400 mr-2"></div>
function lighting(code) {
fetch(`API`)
.then((response) => {
if (!response.ok) {
throw new Error(
`This is an HTTP error: The status is ${response.status}`
);
}
return response.json();
})
.then((actualData) => {
return (actualData.used ? red_light : green_light)
})}
const MembershipLight = (code) => {
return (
lighting(code)
);
};
export default MembershipLight;
但是页面变成空白,我做错了哪一部分?
我尝试使用console.log 和actualData,它显示响应的整个部分,包括used 和true/false,但是当我console.log("actualData.used") 时,它在控制台中显示undefined。
actureData(来自邮递员)
[
{
"used": true,
"create_date": "1644490502",
"update_date": "1666694655"
}
]
【问题讨论】:
-
你也可以加
actualData吗? -
应该是
actualData[0].used ? red_light : green_light -
我认为这行不通
-
它不会因为你没有从函数返回任何东西,如下面的答案......