fetching from an API in react

1 day ago 1
ARTICLE AD BOX

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 ?

Read Entire Article