this is my react hooks code
react hooks state is not update in something function
it is update in useEffect(()=>{...},[count])
but it was not update in getContent
i don't know why this state is not update
if i print a count, it print right in useEffect, but getContent() is not
const [content,contentChange] = useState([]);
const [count,countChange] = useState(0);
useEffect(()=>{console.log(count)},[count])
const getContent = () => {
axios.get(`http://10.156.147.200:3000/api/main/post/${count}`,{
headers:{
"x-access-token":access_token,
}
})
.then((res)=>{
const newData = res.data.post;
let buffer;
buffer = newData.map((e)=>{
return {
isMine:e.isMine,
isLike:e.isLike,
content:e.post.content,
nick:e.post.nick,
img:e.post.img,
id:e.post._id,
profile:e.profile,
}
})
contentChange([...content,...buffer]);
})
.catch((err)=>{
if(err.response.status === 403){
refreshAccessToken();
}
})
}
const scrollEvent = () => {
const { innerHeight } = window;
const { scrollHeight } = document.body;
const scrollTop =
(document.documentElement && document.documentElement.scrollTop) ||
document.body.scrollTop;
if (scrollHeight - innerHeight - scrollTop < 10) {
getContent();
countChange(count+1);
}
}