Data Fetching Using React Query
Why prefer React Query over Redux?
Published in
4 min readJul 15, 2021
Please subscribe my youtube channel: FrontEnd Interview Preparation: https://www.youtube.com/channel/UC-elmWUfbcbmvuhlS12nCtg
I’ve always believed there must be a better way to do the same with React JS. Although I have been using Redux to handle data fetching in projects.
Why React Query?
In redux, each fetch operation requires the developer to create:
- three actions (fetch request, succeeded, failed)
- three reducers (fetch request, succeeded, failed)
- one middleware (usually redux-saga or redux-thunk)
Writing all this code takes a lot of time, even for a simple API call.
Redux process for a fetch operation:
- Dispatch an Action
fetchSomething()
from within the Component - This Action hits the Reducer which will update the
store
with something likeisLoading: true
- The Component re-renders and displays a loader
- In the meanwhile, the Action hits the Middleware which takes care of calling the API
- The API returns the response to the Middleware, which dispatches another Action…