I want to ask: Why userPost is empty?
function Feed() {
const [userPost, setUserPost] = useState([]);
useEffect(() => {
const userId = localStorage.getItem("userID");
const getUserPost = () => {
axios
.get(`http://localhost:5000/api/posts/profile/${userId}`)
.then((res) => {
console.log(res.data.posts);
setUserPost(res.data.posts);
console.log(userPost);
})
.catch((err) => {
console.log(err.response);
});
};
getUserPost();
}, []);
console.log(userPost);
return (<></>)
}
Result: console.log(res.data.posts)
(3) [{…}, {…}, {…}]
0: {_id: '6220c9c7c1750a6ec3618165', userId: {…}, …}
1: {_id: '6220d26dc1750a6ec36181a7', userId: {…}, …}
2: {_id: '6221de326c2cc78a2b758a63', userId: {…}, …}
length: 3
[[Prototype]]: Array(0)
Result: console.log(userPost)
[]
length: 0
[[Prototype]]: Array(0)
Thanks.