{"id":57964,"date":"2023-08-17T17:33:12","date_gmt":"2023-08-17T12:03:12","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=57964"},"modified":"2023-08-30T17:49:45","modified_gmt":"2023-08-30T12:19:45","slug":"a-beginners-guide-to-redux-toolkit-query-rtk-query","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/a-beginners-guide-to-redux-toolkit-query-rtk-query\/","title":{"rendered":"A Beginner\u2019s Guide to Redux Toolkit Query\/RTK Query"},"content":{"rendered":"<p><span style=\"font-weight: 400;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-57959\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/1_pAmejif6D9cxdQxWMrDjtg-1-300x169.jpg\" alt=\"\" width=\"383\" height=\"216\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/1_pAmejif6D9cxdQxWMrDjtg-1-300x169.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/1_pAmejif6D9cxdQxWMrDjtg-1-1024x576.jpg 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/1_pAmejif6D9cxdQxWMrDjtg-1-768x432.jpg 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/1_pAmejif6D9cxdQxWMrDjtg-1-624x351.jpg 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/1_pAmejif6D9cxdQxWMrDjtg-1.jpg 1200w\" sizes=\"(max-width: 383px) 100vw, 383px\" \/><\/span><\/p>\n<p><span style=\"font-weight: 400;\">RTK Query is a set of utilities, a powerful data fetching and caching tool. It is an additional package provided by Redux Toolkit that enhances Redux by adding data fetching and caching capabilities.\u00a0 Data fetching and state management are simple to incorporate in the Redux application by the smooth integration with Redux Toolkit.<\/span><\/p>\n<h3><b>Key Aspects of RTK Query &#8211;<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><\/h3>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Fetch loading<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Error handling<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Automatic Caching<\/span><\/li>\n<\/ul>\n<h3><b>RTK Query primarily consists of two API\u2019s &#8211;\u00a0<\/b><\/h3>\n<ul>\n<li><span style=\"font-weight: 400;\">createApi()<\/span><\/li>\n<li><span style=\"font-weight: 400;\">fetchBaseQuery()<\/span><\/li>\n<\/ul>\n<p><em><b>createApi()<\/b><\/em><span style=\"font-weight: 400;\"> &#8211; To use RTK Query we must construct an <strong><em>\u2018API Slice\u2019<\/em> <\/strong>centrally, in which all the API endpoints that our application requires are listed. This \u2018API Slice\u2019 structure consists of all the Redux logic that enables us to fetch and cache data easily, <\/span><span style=\"font-weight: 400;\">createApi() takes an object parameter with<strong><em> {baseQuery, tagTypes, reducerPath, endpoints}<\/em><\/strong> as the configuration options. <\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><em><b>fetchBaseQuery()<\/b><\/em><span style=\"font-weight: 400;\"> &#8211; It is a compact fetch wrapper designed to streamline API requests &amp; responses in Redux Toolkit and is passed to the <\/span><strong><i>baseQuery <\/i><\/strong><span style=\"font-weight: 400;\">field in the createApi().<\/span><\/p>\n<h3><b>Query and Mutation Endpoints\u00a0 &#8211;\u00a0<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">In RTK Query, The API endpoints are defined by returning the object inside the endpoints section of the createApi().All the endpoints are defined ahead of time and are created against the same baseUrl specified inside the <em><strong>baseUrl<\/strong><\/em> field of the <strong><em>fetchBaseQuery().<\/em><\/strong><\/span><\/p>\n<p><span style=\"font-weight: 400;\">There are two types of endpoints &#8211;\u00a0 <\/span><em><b>Query and Mutation<\/b><\/em><span style=\"font-weight: 400;\"><em>.<\/em> Query Endpoint is used while fetching or caching data from the server and Mutation is used while sending an update to the server. All the fields of the endpoints are defined using the <\/span><em><b>builder.query() or builder.mutation()<\/b><\/em><span style=\"font-weight: 400;\"> method.<\/span><\/p>\n<h3><b>Redux Toolkit Hooks &#8211;\u00a0<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">RTK Query automatically generates hooks for every endpoint defined, based on the name of the endpoint in the service definition. These auto-generated hooks can be directly imported inside the UI component for data-fetching.\u00a0<\/span><\/p>\n<h3><b>Caching Data using RTK Query &#8211;\u00a0<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">In order to prevent re-fetching the same data repeatedly, caching is used. Thus, the caching functionality allows to reuse the already fetched data or cached data until the database is not changed.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">But we may need not to use the cached data or fetch fresh data according to the use case. So, we use <\/span><em><b>Cache Invalidation<\/b><\/em><span style=\"font-weight: 400;\"> to re-fetch the data.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Cache Invalidation is achieved using the\u00a0 &#8211;\u00a0<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><em><b>tagTypes<\/b><\/em><span style=\"font-weight: 400;\"> &#8211; a tag or name is given to an apiSlice to manage the caching and invalidation behavior.<\/span><\/li>\n<li><b><em>provideTags<\/em> <\/b><span style=\"font-weight: 400;\">&#8211; a query consists of a provide tag that determines which tag is attached to the cached data returned by the query<\/span><\/li>\n<li><em><b>invalidateTags<\/b><\/em><span style=\"font-weight: 400;\"> &#8211; a mutation consists of an invalidate tag that determines which cached data is to be invalidated or re-fetched.<\/span><\/li>\n<\/ul>\n<blockquote><p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-57954\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-image.png\" alt=\"\" width=\"475\" height=\"250\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-image.png 880w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-image-300x158.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-image-768x404.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-image-624x328.png 624w\" sizes=\"(max-width: 475px) 100vw, 475px\" \/><\/p><\/blockquote>\n<h3><b>CRUD Operations using RTK Query &#8211;<\/b><\/h3>\n<p><b><i>Step 1. Create a React Project\u00a0<\/i><\/b><\/p>\n<blockquote><p><span style=\"font-weight: 400;\">npx create-react-app demo-app<\/span><\/p><\/blockquote>\n<p><b><i>Step 2. Install required libraries\u00a0<\/i><\/b><\/p>\n<blockquote><p><span style=\"font-weight: 400;\">npm install react-redux @reduxjs\/toolkit bootstrap<\/span><\/p><\/blockquote>\n<p><b><i>Step 3. Create a mock JSON Server.\u00a0<\/i><\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Create a json file -&gt; \u2018db.json\u2019 in the application<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Install json server using\u00a0 -&gt; npm install -g json-server<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Start the json server using -&gt; json-server -\u2013watch db.json -\u2013port PORT_NUMBER<\/span><\/li>\n<\/ul>\n<p><b>S<\/b><b><i>tep 4. Create an API Slice<\/i><\/b><\/p>\n<blockquote><p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-57973\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-8.png\" alt=\"\" width=\"540\" height=\"261\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-8.png 863w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-8-300x145.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-8-768x371.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-8-624x302.png 624w\" sizes=\"(max-width: 540px) 100vw, 540px\" \/><\/p><\/blockquote>\n<p><b><i>Step 5. Connect API Slice to Redux Store<\/i><\/b><\/p>\n<blockquote><p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-57968\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-3.png\" alt=\"\" width=\"551\" height=\"299\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-3.png 796w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-3-300x163.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-3-768x417.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-3-624x339.png 624w\" sizes=\"(max-width: 551px) 100vw, 551px\" \/><\/p><\/blockquote>\n<p><b><i>Step 6. Retrieve data using Query Endpoint<\/i><\/b><\/p>\n<blockquote><p><span style=\"font-weight: 400;\">This Query Endpoint is used to fetch all the data from the database i.e db.json file and display a list of all the list items. <\/span><span style=\"font-weight: 400;\">Here, <\/span><span style=\"font-weight: 400;\">useGetProductsQuery<\/span><span style=\"font-weight: 400;\"> is an auto-generated query hook that provides data, isLoading, isSuccess, isError, error properties. Data property of the hook can be used to fetch &amp; display the data inside the UI component.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-57969\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-4.png\" alt=\"\" width=\"544\" height=\"305\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-4.png 897w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-4-300x168.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-4-768x430.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-4-624x349.png 624w\" sizes=\"(max-width: 544px) 100vw, 544px\" \/><\/p><\/blockquote>\n<p><b><i>Step 7. Update Operation using Mutation Endpoint<\/i><\/b><\/p>\n<blockquote><p><span style=\"font-weight: 400;\">Mutation endpoint is used to add a new item in the list and delete an item. <\/span><span style=\"font-weight: 400;\">useAddProductMutation<\/span><span style=\"font-weight: 400;\"> and <\/span><span style=\"font-weight: 400;\">useDeleteProductMuatation<\/span> <span style=\"font-weight: 400;\">are the mutation hooks.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-57970\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-5.png\" alt=\"\" width=\"552\" height=\"538\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-5.png 937w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-5-300x293.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-5-768x750.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-5-624x609.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-5-24x24.png 24w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-5-48x48.png 48w\" sizes=\"(max-width: 552px) 100vw, 552px\" \/><\/p><\/blockquote>\n<p><b><i>Step 8. Use RTK hooks inside Components<\/i><\/b><\/p>\n<blockquote><p><span style=\"font-weight: 400;\">The RTK Query and Mutation hooks are imported inside the required UI React Components for fetching and updating the data.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-57971\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-6.png\" alt=\"\" width=\"553\" height=\"568\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-6.png 896w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-6-292x300.png 292w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-6-768x789.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-6-624x641.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-6-24x24.png 24w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-6-48x48.png 48w\" sizes=\"(max-width: 553px) 100vw, 553px\" \/><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-57972\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-7.png\" alt=\"\" width=\"559\" height=\"341\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-7.png 1182w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-7-300x183.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-7-1024x625.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-7-768x468.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/rtk-1-7-624x381.png 624w\" sizes=\"(max-width: 559px) 100vw, 559px\" \/><\/p><\/blockquote>\n<h4><b>With the above RTK Query implementation the following Redux implementation steps have been eliminated &#8211;\u00a0<\/b><\/h4>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">No need to create multiple action creators for each fetch request, as there is a single API service for the entire baseUrl.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Creating multiple reducers have been removed.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">We can retrieve data directly without having to dispatch actions or use selectors inside the UI components.\n<p><\/span><\/li>\n<\/ul>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>RTK Query is a set of utilities, a powerful data fetching and caching tool. It is an additional package provided by Redux Toolkit that enhances Redux by adding data fetching and caching capabilities.\u00a0 Data fetching and state management are simple to incorporate in the Redux application by the smooth integration with Redux Toolkit. Key Aspects [&hellip;]<\/p>\n","protected":false},"author":1619,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":249},"categories":[3429,3038],"tags":[4064,4390,5328],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57964"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1619"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=57964"}],"version-history":[{"count":3,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57964\/revisions"}],"predecessor-version":[{"id":58208,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57964\/revisions\/58208"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=57964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=57964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=57964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}