1

I wanted to reload data from the server ever time a certain tab is tapped on. useEffect was however not doing this by itself as I expected it would. I found useFocusEffect however

which seems to do what I want.

import { useFocusEffect } from "@react-navigation/native";

  useFocusEffect(
    React.useCallback(() => {
      request(uid);
      return () => {
        alert("Screen was unfocused");
        // Do something when the screen is unfocused
        // Useful for cleanup functions
      };
    }, [])
  );

So, this works great in that every time I tap on that tab it fetches the data like I want. However, in the example code it has the 'useful for cleanup functions'. Is there something I should be doing there? I am basically fetching a list of users.

Iggy's Pop
  • 589
  • 1
  • 6
  • 24
  • Does [this](https://stackoverflow.com/q/54954385/8839059) answer your question? – Rafael Tavares Aug 01 '20 at 20:35
  • I don't know. It depends if the answer is that I need to put some sort of abort controller or cancellation token in there? – Iggy's Pop Aug 02 '20 at 17:29
  • Yes. If your component unmounts before completing the fetch request, you are 1) making an useless fetch; and 2) trying to update an unmounted component. So you should use this `return` to abort your request – Rafael Tavares Aug 02 '20 at 22:20
  • Okay, I just have to figure out what to put in there now though as I am using ApiSauce which is a wrapper for Axios. – Iggy's Pop Aug 03 '20 at 08:58

0 Answers0