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

341
Visualizações
Async/Await in useEffect(): how to get useState() value?

I have the following code snippet. Why is my limit always 0 in my fetchData? If I were to console.log(limit) outside of this function it has the correct number. Also If I dont use useState but a variable instead let limit = 0; then it works as expected

I also added limit as a dependency in useEffect but it just keeps triggering the function

  const [currentData, setData] = useState([]);
  const [limit, setLimit] = useState(0);

const fetchData = async () => {
    console.log(limit);
    const { data } = await axios.post(endpoint, {
      limit: limit,
    });
    setData((state) => [...state, ...data]);
    setLimit((limit) => limit + 50);
  };

  useEffect(() => {
    fetchData();
    window.addEventListener(`scroll`, (e) => {
      if (bottomOfPage) {
        fetchData();
      }
    });
  }, []);
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

When you pass an empty dependency array [] to useEffect, the effect runs only once on the initial render:

If you pass an empty array ([]), the props and state inside the effect will always have their initial values.

If you want to run an effect and clean it up only once (on mount and unmount), you can pass an empty array ([]) as a second argument. This tells React that your effect doesn’t depend on any values from props or state, so it never needs to re-run. This isn’t handled as a special case — it follows directly from how the dependencies array always works.

useEffect docs

The initial state of limit is 0 as defined in your useState call. Adding limit as a dependency will cause the effect to run every time limit changes.

about 4 years ago · Juan Pablo Isaza Relatório

0

One way to get around your issue is to wrap the fetchData method in a useCallback while passing the limit variable to the dependency array.

You can then pass the function to the dependency array of useEffect and also return a function from inside of useEffect that removes event listeners with outdated references.

You should also add a loading variable so that the fetchData function doesn't get called multiple times while the user is scrolling to the bottom:

const [currentData, setData] = useState([]);
const [limit, setLimit] = useState(0);
const [loading, setLoading] = useState(false);

const fetchData = useCallback(async () => {
    console.log(limit);
    // Prevent multiple endpoint calls when scrolling near the end with a loading state
    if (loading) {
        return;
    }

    setLoading(true);
    const { data } = await axios.post(endpoint, { limit });
    setData((state) => [...state, ...data]);
    setLimit((limit) => limit + 50);
    setLoading(false);
}, [limit, loading]);

// Make the initial request on the first render only
useEffect(() => {
    fetchData();
}, []);

// Whenever the fetchData function changes update the event listener
useEffect(() => {
    const onScroll = (e) => {
        if (bottomOfPage) {
            fetchData();
        }
    };

    window.addEventListener(`scroll`, onScroll);

    // On unmount ask it to remove the listener
    return () => window.removeEventListener("scroll", onScroll);
}, [fetchData]);
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