-1

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);
        }
    }
sema0710
  • 25
  • 5

2 Answers2

2

it is just because the getContent run first before the count state change try to add setTimeout in getContent() what happens in useEffect(()=> {....}, [count]) why it is updated it is only because of array count [count] .. runs the code again if count change so it only runs when count change while getContent run while the count is still in its previous state. https://reactjs.org/docs/hooks-effect.html

  const getContent = () => {

    setTimeout(()=> {
        //fetching data

    }, 1000) // try to modify it 

  }

or try this

     useEffect(() => {
         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;
                }
     }, [count])

You should call countChange(count+1); before getContent();

Abu Dujana Mahalail
  • 1,547
  • 1
  • 8
  • 21
1

Use countChange(count+1); inside the getContent after contentChange function like this

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]);
            countChange(count+1); //add line
        })
        .catch((err)=>{
            if(err.response.status === 403){
                refreshAccessToken();
            }
        })
    }

And remove countchange in scroll event

prasanth
  • 22,145
  • 4
  • 29
  • 53