Run fetch at regular intervals using react
Asked Answered
A

2

5

I have a grid with different react components, all independant - in that they fetch their own data and display it.

I wanted to somehow make them automatically refetch and update every 15 minutes. My first idea was a HOC, but with the newer hooks and functional components I tried something using hooks.

So I already was able to create a custom hook - to handle the fetching of data:

const useFetching = (url) => {
  const [loading, setLoading] = useState(true);
  const [data, setData] = useState({});
  const [error, setError] = useState(null);
  const [reload, setReload] = useState(true);

  useEffect(() => {
    const fetchData = async () => {
      try {
        const response = await fetch(url);

        if (!response.ok) {
          throw new Error('Something went wrong');
        }

        const json = await response.json();

        if (json) {
          setData(json);
        }
      } catch (error) {
        setError(error);
      }

      setLoading(false);
    }

    fetchData();

  }, [url, reload]);

  return {
    loading,
    data,
    error
  };
};

Then two components using this:

const App1 = () => {
  const {loading, data, error} = useFetching(
    "https://hn.algolia.com/api/v1/search?query=redux"
  );

  if (loading || error) {
    return <p>{loading ? "Loading..." : error.message}</p>;
  }

  return (
    <ul>
      {data.hits.map(hit =>
        <li key={hit.objectID}>
          <a href={hit.url}>{hit.title}</a>
        </li>
      )}
    </ul>
  );
};


const App2 = () => {
  const {loading, data, error} = useFetching(
    "https://hn.algolia.com/api/v1/search?query=balls"
  );

  if (loading || error) {
    return <p>{loading ? "Loading..." : error.message}</p>;
  }

  return (
    <ul>
      {data.hits.map(hit =>
        <li key={hit.objectID}>
          <a href={hit.url}>{hit.title}</a>
        </li>
      )}
    </ul>
  );
};

ReactDOM.render(<><App1 /><App2/></>, document.getElementById('root'));

This works fine - using the custom hook. So I was thinking something in the lines of:

Set up an interval timer which runs ever 15 minutes and changes the 'reload' state flag - to trigger a refetch and update. Also make sure the interval timer is unset on component unload.

To access the state and the setReload method - I placed the timer inside of the useFetching component - but that didn't work as it fired up multiple timers. I think I had 14 timers for only two components.

I'm really unsure of how to proceed - I would like one timer only handling the refresh of all components - at the same time I want the components to be independant and not have to share anything with an external timer. It would be fine with one timer per component - as long as it properly destroyed when the component is unloaded - and it would be nice if this mechanism is built into the useFetching hook.

Is there a much better way to build this - that I'm not thinking of?

Suggestions much appreciated!

Alert answered 2/12, 2019 at 13:11 Comment(0)
P
7

You need a useInterval hook. This post explains exactly why and how: https://overreacted.io/making-setinterval-declarative-with-react-hooks/

Partisan answered 2/12, 2019 at 14:8 Comment(1)
That's a really great article, thank you! Not sure why but it worked fine using setInterval - perhaps because I was always running the same thing and using current stateAlert
H
6

Since useFetch encapsulates the fetching logic for a single component, makes sense that each component manages it's own interval. You can achieve that by declaring on mount an interval which will be triggered fifteen minutes later.

useEffect(() =>{
    let interval = setInterval(() => setReload(true), (1000 * 60 * 15))
    //destroy interval on unmount
    return () => clearInterval(interval)
})

More about useEffect (including effect cleanup as above) in the documentation.

Hulton answered 2/12, 2019 at 13:16 Comment(2)
haha Yes. The cleanup is there. Thanks for enhancing the answer!Hulton
Thanks! With useEffect(() =>{ const interval = setInterval(() => setReload(state => !state), 3000); console.log('setup interval' + interval); return () => clearInterval(interval); }, []) It works! Thanks! so I had to toggle reload every time - and I had to run this only once.Alert

© 2022 - 2024 — McMap. All rights reserved.