Blog>
Snippets

Debugging with TanStack Ranger's Development Tools

Explain how to utilize TanStack Ranger’s built-in development tools for debugging state management issues, including setting breakpoints and inspecting state changes.
import { useQuery } from 'tanstack/react-query';

// Example query to fetch data
const { data, error, isError, isLoading } = useQuery(['todos'], fetchTodoList);
This code imports the useQuery hook from TanStack Query (formerly React Query) and sets up a basic query to fetch data. It specifies a unique key ['todos'] for the query and a function fetchTodoList that fetches the data. The result of the query includes data, an error object, and flags for loading and error states.
import { QueryClient, QueryClientProvider, useQueryErrorResetBoundary } from 'tanstack/react-query';

const queryClient = new QueryClient();

function App() {
  const { reset } = useQueryErrorResetBoundary();

  return (
    <QueryClientProvider client={queryClient}>
      {/* Application components that will use TanStack Query */}
    </QueryClientProvider>
  );
}
This snippet demonstrates how to set up the QueryClient and QueryClientProvider for TanStack Query, and use the useQueryErrorResetBoundary hook for error handling. The QueryClient initializes querying capabilities, while the QueryClientProvider makes the query functionality available throughout your component tree.
queryClient.getQueryData(['todos'])
This code accesses cached query data for the query key ['todos']. It's a way to directly inspect the currently cached data for debugging purposes.
queryClient.setQueryData(['todos'], updatedTodos => ({ ...updatedTodos, ...newData }));
This line of code demonstrates how to programmatically update the cache for a specific query key ['todos']. It retrieves the current cache, updates it with new data, and saves the updated cache. This can be valuable for debugging state changes or simulating how state updates would affect the application.