![]() ![]() ![]() In my mind, you had the “data layer” part of your app, which handles fetching the data. But when I first saw the announcement, I couldn’t help but think that this isn’t Redux’s job. RTK Query makes it incredibly simple to handle your app’s data fetching in a maintainable way. services/jokes.js import ) But Why Does Redux Care About Data Fetching? To see how simple data fetching becomes using RTK Query, here is a basic example of fetching a joke from an API and rendering it.Ī lot is going on here, so let’s break it down. RTK Query is “designed to simplify common cases for loading data in a web application.” Now, instead of potentially five files to fetch data and cache it in the Redux store, we need one. And it’s up to the developer to make sure these are done in a reusable way, to make it easy to extend and maintain. It’s up to the developer to handle loading states, error states, caching, polling, optimistic updates, etc. Typically, data fetching is done using either Redux Thunk or Redux-Saga.ĭespite this, data fetching is still a manual process, which leaves plenty of room for error and edge cases. To get around this, Redux provides a space for middleware - a way to intercept dispatched actions before they are received by the reducer. Any asynchronicity has to happen outside the store.” It only knows how to synchronously dispatch actions, update the state by calling the root reducer function, and notify the UI that something has changed. Redux has no built-in way to handle any side effects, including asynchronous code.Īs the docs themselves say, “By itself, a Redux store doesn’t know anything about async logic. It’s hard in any application, but it’s especially tricky in Redux apps. In this post, I want to explain why I’m excited for RTK Query and answer my original question, “but why?” “That’s Cool” When I first saw the announcement, my reaction was, “That’s cool… but why?” Why would a state management library need to handle data fetching? RTK Query is intended to make data fetching and caching with Redux as easy as possible. Now, they’ve added “the newest member of the Redux family”, RTK Query. To address this, the Redux team has released Redux Toolkit (RTK): an opinionated toolset for writing Redux applications. ![]() Even Dan Abramov, the creator of Redux, complained about this when he returned to a Redux codebase: Redux is the most used state management library for frontend apps - 28% of React apps on Github use it! Despite this, there remains one complaint so common it’s almost cliché: boilerplate. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |