Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions country-explorer/src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import reactLogo from '../assets/react.svg';
import viteLogo from '../assets/vite.svg';
import './App.css';



function App() {
const [count, setCount] = useState(0);

Expand Down
23 changes: 23 additions & 0 deletions country-explorer/src/hooks/useCountries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useQuery } from '@tanstack/react-query';
import { getCountriesByRegion } from '../api/countries';

const DEFAULT_REGION = 'europe';

// Centralizes the query key so it's built the same way everywhere it's
// needed (e.g. a future manual refetch/invalidate call), instead of a
// duplicated inline array.
export const countriesQueryKey = (region: string) => ['countries', region] as const;

export function useCountries(region: string = DEFAULT_REGION) {
const { data, isLoading, isError, error } = useQuery({
queryKey: countriesQueryKey(region),
queryFn: () => getCountriesByRegion(region),
});

return {
countries: data ?? [],
isLoading,
isError,
error,
};
}
4 changes: 2 additions & 2 deletions country-explorer/src/lib/queryClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QueryClient } from '@tanstack/react-query'
import { QueryClient } from '@tanstack/react-query';

export const queryClient = new QueryClient({
defaultOptions: {
Expand All @@ -10,4 +10,4 @@ export const queryClient = new QueryClient({
retry: 1,
},
},
})
});
14 changes: 7 additions & 7 deletions country-explorer/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { QueryClientProvider } from '@tanstack/react-query'
import './index.css'
import App from './components/App'
import { queryClient } from './lib/queryClient'
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { QueryClientProvider } from '@tanstack/react-query';
import './index.css';
import App from './components/App';
import { queryClient } from './lib/queryClient';

createRoot(document.getElementById('root')!).render(
<StrictMode>
<QueryClientProvider client={queryClient}>
<App />
</QueryClientProvider>
</StrictMode>,
)
);