Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

340
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda