Fetching from API in react #188137
Replies: 3 comments
-
|
Yeah, TanStack Query is basically the go-to for data fetching in React nowadays. You're not wrong. Managing fetches manually with useEffect + useRef flags to prevent duplicates gets messy fast. Using useQuery everywhere for reads and useMutation for things like your AddProduct.tsx (POST requests) is totally fine and pretty standard in production apps. It's not overkill, it's just the right tool for the job. The only thing I'd add: it's still worth understanding why the duplication happens with raw useEffect (Strict Mode double-mounting, missing cleanup, dependency issues), so you're not just hiding problems behind a library. But once you get that, there's no reason to keep fighting those issues manually when TanStack Query handles it all for you. |
Beta Was this translation helpful? Give feedback.
-
|
Hi MahmoudGEA2005, TanStack Query is definitely the recommended approach for data fetching in React. It handles deduplication, caching, and background updates out of the box. For your AddProduct component, use |
Beta Was this translation helpful? Give feedback.
-
Is TanStack Query the Professional Standard for API Fetching in React?Short answer: yes, and you've already figured out the hard way why it exists. Let me break this down properly. The Root of the Duplication ProblemBefore jumping straight to TanStack Query, it's worth understanding what's actually going on under the hood when you fetch with raw The duplication you're seeing isn't always StrictMode's fault (though StrictMode does intentionally double-invoke effects in development to surface bugs). The real culprits are usually: 1. Missing cleanup functions If the component unmounts before the request finishes, the state update still fires, causing either a memory leak warning or a stale update on a re-mounted component. 2. Dependency array issues Objects and arrays in dependency arrays are compared by reference, not value. So if 3. The
And now you've got manual cache management, which is exactly what you shouldn't be doing by hand. What TanStack Query Actually SolvesTanStack Query isn't just a "prevent duplicate requests" tool. It's a full async state manager. Here's what it handles that you'd otherwise implement manually:
Using It in an
|
| Type | Tool |
|---|---|
| Server data (GET) | useQuery |
| Server mutations (POST/PUT/DELETE) | useMutation |
| UI / local state | useState / useReducer |
| Complex shared client state | Zustand, Redux, Jotai |
Setup (if you haven't already)
// main.tsx import { QueryClient, QueryClientProvider } from '@tanstack/react-query';const queryClient = new QueryClient();
ReactDOM.createRoot(document.getElementById('root')!).render(
<QueryClientProvider client={queryClient}>
<App />
</QueryClientProvider>
);
You can also install the devtools during development — they give you a live view of every query, its cache status, and how many observers are subscribed to it:
npm install @tanstack/react-query-devtools
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
// inside QueryClientProvider
<ReactQueryDevtools initialIsOpen={false} />
Final Take
The fact that you went through the useEffect + useRef pain before reaching TanStack Query actually puts you in a better spot than most. You understand why the problem exists, not just that the library fixes it. That makes you significantly better at debugging edge cases when they inevitably come up, even inside TanStack Query.
Is it professional to use TanStack Query for API fetches across the board? It's more than professional — at this point, not using it (or something equivalent like SWR) in a production React app is what raises eyebrows.
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Select Topic Area
Question
Body
so to fetch from an API in react we can either use the useEffect(), or a combination of useEffect() and useCallback(), but there is a very annoying problem that I see most of the time where we get requests duplication, even though StrictMode was already remvoed from main.tsx, then you start creating refereneces with useRef() to check if the data is stale and decide when to make the request again, especially when we have states that get intiailaized with null then becomes 0
so I learned about the useQuery from TanStack, basically it is used when you want to avoid unneccery fetches like when you switch tabs , but it turned out that it sovles the whole fetches duplication issue with minimal code, so is it considered more professional to use it for API fetches everywhere, like in an AddProduct.tsx component ?
Beta Was this translation helpful? Give feedback.
All reactions