R
R
Res1dentEvil2021-12-02 23:38:10
React
Res1dentEvil, 2021-12-02 23:38:10

How to remove the warning?

Plz tell me how to remove the warning?

61a92878aaeb8165377540.png

I use useEffect

useEffect(() => {
        getTodo()
    }, [getTodo])


and the getTodo function

const getTodo = useCallback(async () => {
        try {
                await axios.get('/api/todo', {
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    params: {userId}
                })
                    .then((response) => {
                        setTodos(response.data)
                    })
        } catch (error) {
            console.log(error)
        }
    }, [userId, setTodos])


There seems to be a solution to a similar situation. https://habr.com/en/post/493496/
But, apparently, I'm doing something wrong, so I could not remove the error.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikolay Matyushkin, 2021-12-03
@Devilz_1

They forgot to add the most important thing from the example - the cleaning function. This is what the error tells you. Clear useEffect before unmounting the component.
in your useEffect at the very beginning, describe the variable, and assign the value false and at the very end add a callback return, this callback will work before the component is unmounted.
let cleanupFunction = false;
return () => cleanupFunction = true;

I
Ivan Ivanovich, 2021-12-03
@IwanQ

useEffect(() => {
  let stopGettingAfterUnmount = false;

  const getTodo = async () => {
    await axios
      .get("/api/todo", {
        headers: {
          "Content-Type": "application/json",
        },
        params: { userId },
      })
      .then((response) => {
        if (stopGettingAfterUnmount) return;

        setTodos(response.data);
      });
  };

  getTodo();

  return () => {
    stopGettingAfterUnmount = true;
  };
}, [userId]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question