我想将我的帖子从 ButterCMS 导入 React,但我不知道如何处理异步问题。
import React, { useState } from "react"
import Butter from "buttercms"
import gradient from "../../images/TealLove.jpg"
export default () => {
const butter = Butter("API_KEY")
const [post, setPost] = useState()
butter.post.list({ page: 1, page_size: 10 }).then(function(response) {
setPost(response.data.data[0])
})
return (
<>
<div className={"section"}>
<div className={"container"}>
<div className={"first-post"}>
<figure className={"image"}>
<img src={gradient} alt=""></img>
</figure>
<div className={"first-post-title"}>
<h2>
{post.title.length > 20
? post.title.slice(0, 85) + " ..."
: post.title}
</h2>
</div>
</div>
</div>
</div>
</>
)
}
每次我运行这个,错误post is undefined都会出现。如何仅在定义帖子后才呈现元素?
德玛西亚99
相关分类