Dženis H.
Dženis H.

Reputation: 7822

How can I use an item (function) outside from UseEffect hook?

This is the code in question:

    useEffect(() => {
    setStart(start + count);
    const fetchImages = () => {
      axios
        .get(`/api/photos?count=${count}&start=${start}`)
        .then((res) => setImages(images, ...res.data));
      fetchImages();
    };
  }, []);

How can I use fetchImages outside of the useEffect hook?

Upvotes: 1

Views: 44

Answers (1)

Vivek_Neel
Vivek_Neel

Reputation: 1353

You need to put fetchImages outside of useEffect and wrap it with useCallback and then you can use fetchImages outside of useEffect and inside the useEffect as well

Upvotes: 1

Related Questions