Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching with react-query and react-router-dom

I'm trying out react-query and want to see it caching. I have a page that gets a list of Books.

Note, some code obviously left out for brevity just trying to show the important parts.

const useBooks = options => {
  return useQuery(['books', options], bookApi.fetchBooks(options));
}

const Books = () => {
  useEffect(() => () => console.log("unmount"), []);
  const [options, setOptions] = useState({}); // options ex: { page: 1, search: "test"}
  const {error, isLoading, data} = useBooks(options);

  return books.map(book => <div>{book.name}</div>;
}

Now this Books is a "page" in my app using react-router-dom:

const App = () => {

  return (
    <Router>
      <>
        <Route exact path="/">
          <Home />
        </Route>
        <Route path="/books">
          <Books />
         </Route>
      </>
     </Router>
    )
  }

When I go back and forth between "Home" and "Books" (by clicking Nav links in my app) I see the books fetch hitting the network every time. I also see "unmount" in the console.

I originally had the Routes wrapped in a Switch, and removed this but the Books component still appears to unmount. I am hoping it will not unmount and I will not see the network fetch, and the books will be pulled from the react-query cache. I also tried making the useBooks hook not use the search options param but the results were the same.

How do I use avoid the network fetch in my scenario?

like image 683
user210757 Avatar asked Jan 28 '21 15:01

user210757


People also ask

Does react query cache?

React Query is a library that has 2 simple hooks which provide fetching, caching and updating asynchronous data in React applications. It was created by open sourcerer Tanner Linsley in 2019, and now it is proven that it is very useful in server state management in React applications.

What is query cache in react query?

The QueryCache is the storage mechanism for React Query. It stores all the data, meta information and state of queries it contains. Normally, you will not interact with the QueryCache directly and instead use the QueryClient for a specific cache.

Should I use react query or SWR?

So if you are building a simple application and want a ready-to-go simple solution then SWR should be your choice. But if you need more control and customization and want to get the most out of the developer tools then definitely go for react-query.

How do I get the query in react dom Router?

Get a single Query String value location.search object: import React from 'react'; import { useSearchParams } from 'react-router-dom'; const Users = () => { const [searchParams] = useSearchParams(); console. log(searchParams); // ▶ URLSearchParams {} return <div>Users</div>; };


1 Answers

react-router will unmount your component when you navigate away from it, yes. But that is on purpose and not the problem.

When a component that uses a react-query hook mounts, it will trigger a fetch (because of mounting). However, if you already have data in the cache for this specific key, you will instantly get that data returned as "stale data", and the fetch will happen in the background only.

If you want to avoid the request going out, you can either:

  • set refetchOnmount to false so that there will be no refetch when the component mounts
  • set a staleTime on your query

I would suggest setting the staleTime: It will tell react-query for how long the data should be considered "fresh". If you set it to 5 minutes, there will be no background-refetch for 5 minutes - the data will always come from the cache directly. staleTime defaults to 0, so you will always get background refetches.

like image 98
TkDodo Avatar answered Oct 19 '22 00:10

TkDodo