Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
Not Able to access updated state in react

I am not able to access my updated state in a function. Here is my scenario:

I have a getImages() function which gets called when either the component loads for the first time or the user reaches to the end of the page while scrolling to fetch new images via API call. On first time component load, I want to give the random page value that's why I am passing a random number from first useEffect hook and when user reaches to the end of page, I want to make use of pageNum state. But every time while reaching to the end of page, the pageNum value is coming as 0 in the API call as well as in the console log. I was expecting the updated pageNum state as everytime in the end of getImages() function, pageNum state is getting incremented by 1. I checked react dev tools and there the pageNum state is updating correctly.

enter image description here

Here is the snippet of my code:

The images state is updating correct, it is making use of existing state and appending new images on every call.

    const [images, setImages] = useState([]);

    const [pageNum, setPageNum] = useState(0);
 
    useEffect(() => {
        getImages(Math.floor(Math.random() * 100) + 1);
    }, []);

    useEffect(() => {
        window.addEventListener("scroll", () => {
            if((window.innerHeight + window.scrollY) === document.body.scrollHeight){
                getImages();
            }
        });
        
    }, []);

    async function getImages(pageNo){
        let response;
        console.log(pageNum);
        if(pageNo === undefined){
            response = await fetch(`<API URL>&page=${pageNum}`);
        }
        else{
            response = await fetch(`<API URL>&page=${pageNo}`);
        }
        const data = await response.json();

        setImages(function(currentState){
            return [...currentState, ...data];
        });
        setPageNum(function(currentState){
            return currentState + 1;
        });
    }

Please let me know if I am missing out anything.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Reason:

The problem happened bc you not put pageNum as useEffect dependencies. because you add event listener at when component mount for first time ( pageNum is 0 that time). And because you not adding pageNum to useEffect dependencies pageNum value will never update.

Solution:

const [images, setImages] = useState([]);

    const [pageNum, setPageNum] = useState(0);
 
    useEffect(() => {
        getImages(Math.floor(Math.random() * 100) + 1);
    }, []);

    useEffect(() => {
        const fetchImgOnCrollHandler = () => {
            if((window.innerHeight + window.scrollY) === document.body.scrollHeight){
                getImages();
            }
        }
        window.addEventListener("scroll", fetchImgOnScrollHandler);
        return () => {
          window.removeEventListener('scroll', fetImgOnScrollHandler);
        }
    }, [pageNum]);

    async function getImages(pageNo){
        let response;
        console.log(pageNum);
        if(pageNo === undefined){
            response = await fetch(`<API URL>&page=${pageNum}`);
        }
        else{
            response = await fetch(`<API URL>&page=${pageNo}`);
        }
        const data = await response.json();

        setImages(function(currentState){
            return [...currentState, ...data];
        });
        setPageNum(function(currentState){
            return currentState + 1;
        });
    }

Now everytime pageNum update. scroll event attached to window will be removed and add new one with new pageNum to it

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda