【发布时间】:2021-03-29 08:37:18
【问题描述】:
所以我正在使用 useEffect 挂钩从数据库中获取我的数据,在我获得该数据后,我想将其设置为 title 和 postBody 的 useState,但它不起作用,因为 useEffect 挂钩“最后”运行,如何可以修吗?
代码:
const [cPost, setCPost] = useState([]);
const postId = id.match.params.id;
useEffect(() => {
axios.get('http://localhost:5000/posts/'+postId)
.then(posts => {
setCPost(posts.data);
console.log("SAS");
})
}, []);
const [title, setTitle] = useState(cPost.title);
const [postBody, setPostBody] = useState(cPost.postBody);
【问题讨论】:
标签: javascript reactjs react-hooks components