<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: dravidjones28</title>
    <description>The latest articles on DEV Community by dravidjones28 (@dravidjones28).</description>
    <link>https://dev.to/dravidjones28</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1119617%2Fdfaca307-8a81-485b-970b-405ab286a377.png</url>
      <title>DEV Community: dravidjones28</title>
      <link>https://dev.to/dravidjones28</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dravidjones28"/>
    <language>en</language>
    <item>
      <title>Getting Started with React Native in 2023: An Comprehensive Guide for Beginners</title>
      <dc:creator>dravidjones28</dc:creator>
      <pubDate>Wed, 09 Aug 2023 09:36:45 +0000</pubDate>
      <link>https://dev.to/dravidjones28/getting-started-with-react-native-in-2023-an-comprehensive-guide-for-beginners-2b3e</link>
      <guid>https://dev.to/dravidjones28/getting-started-with-react-native-in-2023-an-comprehensive-guide-for-beginners-2b3e</guid>
      <description>&lt;p&gt;In the intersection of thriving software engineering demand and the rise of mobile application development, learning React Native in 2023 can be a game-changing move. There's no doubt that the realm of app development has rapidly evolved, making the technology we use today unquestionably different from what it was a decade ago. Let's delve into what makes React Native stand out, and arm you with the knowledge you need to begin your React Native journey in 2023.&lt;/p&gt;

&lt;h2&gt;
  
  
  React Native: An Overview
&lt;/h2&gt;

&lt;p&gt;In the world of mobile app development, React Native, developed by Facebook, has gained immense recognition. Let's understand why:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Code Reusability: With React Native, you can use the same code for deploying apps on different platforms. This drastically reduces development time and costs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Robust Performance: React Native provides almost identical performance to native apps because it uses the same building blocks as traditional iOS and Android apps.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Live Reload Feature: This remarkable feature allows you to see instant results of code changes, significantly improving the debugging process.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How to Start React Native as a Beginner
&lt;/h2&gt;

&lt;p&gt;To jump-start your React Native learning journey in 2023, here are some crucial steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Understand JavaScript Fundamentals: Mastering JavaScript is vital as React Native is built on it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Learn React: Get familiar with React before moving onto React Native. Understanding the foundational concepts will ease your transition.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hands-On Practice: Practice with simple apps before trying more complex projects.&lt;/li&gt;
&lt;li&gt;Stay Updated: With new updates, React Native is evolving. Stay in the loop through React Native’s official documentation and web resources.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Decoding Cons and Props in React Native
&lt;/h2&gt;

&lt;p&gt;In React Native, 'props' and 'state' (often referred to as 'cons') are vital concepts. Let’s unravel them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Props: Short for properties, props are arguments passed into React components. They help in making components reusable and dynamic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;State or Cons: While props are fixed throughout a component’s lifetime, the state or 'cons' can change. It is mutable and used for components to keep track of changing information.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  React Native vs Flutter: The Key Differences
&lt;/h2&gt;

&lt;p&gt;Choosing between React Native and Flutter for app development can be challenging. Here’s how they contrast:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Language: React Native uses JavaScript, while Flutter uses Dart, a less popular language.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Performance: Although both platforms offer good performance, Flutter has a slight edge due to its direct compilation to native code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Community Support: React Native, being older, has wider community support compared to Flutter.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;UI Components: Flutter offers a comprehensive variety of UI components, while React Native is more limited in this regard.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Entering the world of mobile app development through React Native can be a promising start. As 2023 approaches, arming yourself with an understanding of its concepts, pros, and comparisons with alternatives like Flutter, can set you up for success.&lt;/p&gt;

&lt;p&gt;With React Native, you’re not just learning a toolset, but stepping into a fast-paced, exciting realm that's shaping the very way we interact with technology. Ready to step into the future of mobile app development with React Native?&lt;/p&gt;

&lt;p&gt;Author’s Note: This article is intended to provide general guidance on getting started with React Native in 2023. As every learning journey is unique, I recommend leveraging a variety of resources to get the most comprehensive understanding. Happy coding!&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>react</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
    <item>
      <title>How to Implement Infinite Queries in React Query</title>
      <dc:creator>dravidjones28</dc:creator>
      <pubDate>Wed, 19 Jul 2023 07:04:18 +0000</pubDate>
      <link>https://dev.to/dravidjones28/how-to-implement-infinite-queries-in-react-query-22e6</link>
      <guid>https://dev.to/dravidjones28/how-to-implement-infinite-queries-in-react-query-22e6</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F93a352mt64e00yzrmtjn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F93a352mt64e00yzrmtjn.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
We're goona have a button for loading more data.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F58el0eh4jrl6b34axuie.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F58el0eh4jrl6b34axuie.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, in a real-world application, we often want to load more data once the user scrolls down and reaches the bottom of the page.&lt;/p&gt;

&lt;p&gt;We'll explore that later and focus only on loading more data.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import { useInfiniteQuery } from "@tanstack/react-query";
import axios from "axios";

interface Users {
  id: number;
  name: string;
}

interface UserQuery {
  pageSize: number;
}

const useUsers = (query: UserQuery) =&amp;gt;
  useInfiniteQuery&amp;lt;Users[], Error&amp;gt;({
    queryKey: ["users", query],
    queryFn: () =&amp;gt;
      axios
        .get("https://jsonplaceholder.typicode.com/users", {
          params: {
            _page: 2,
            _limit: query.pageSize,
          },
        })
        .then((res) =&amp;gt; res.data),
  });

export default useUsers;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  How to calculate page number?
&lt;/h2&gt;

&lt;p&gt;(useInfiniteQuery) has a function called (getNextPageParam) and it has two parameters.&lt;br&gt;
1.) lastPage&lt;br&gt;
2.) allPages&lt;/p&gt;

&lt;p&gt;1.) (lastPage) is the array of Users.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq3tg4ea5uxn827717ohx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq3tg4ea5uxn827717ohx.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2.) (allPages) is the two dimensional array of Users&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw2a9wgeobhtwxhjqhnhl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw2a9wgeobhtwxhjqhnhl.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyqfnh7jm3qs7x8un1xhn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyqfnh7jm3qs7x8un1xhn.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Each element in this array is the Array of users.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This parameter (allPages) contains the data for all Pages.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So (getNextPageParam) function should return the next page number.&lt;br&gt;
for example- if you are in page one we should return page 2.&lt;/p&gt;

&lt;p&gt;So, how do you calculate this?&lt;/p&gt;

&lt;p&gt;(allPages) parameters contains data for all Pages.&lt;/p&gt;

&lt;p&gt;so you can return allPages.length + 1 as a next page number.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const useUsers = (query: UserQuery) =&amp;gt;
  useInfiniteQuery&amp;lt;Users[], Error&amp;gt;({
    queryKey: ["users", query],
    queryFn: ({ pageParam = 1 }) =&amp;gt;
      axios
        .get("https://jsonplaceholder.typicode.com/users", {
          params: {
            _page: pageParam,
            _limit: 4,
          },
        })
        .then((res) =&amp;gt; res.data),

    getNextPageParam(lastPage, allPages) {
      return allPages.length + 1;
    },
  });


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;But at some point we are going reach the end the list. we don't want to increment the page number forever?&lt;/p&gt;

&lt;p&gt;With JSON Placeholder, if we request data for a page that doesn't exist, we can get an empty array. At some point, lastPage is going to be an empty array.&lt;/p&gt;

&lt;p&gt;We can give expression like this&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

 getNextPageParam(lastPage, allPages) {
      return lastPage.length &amp;gt; 0 ? allPages.length + 1 : undefined;
    },


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;But this is not the way to implement this logic. It varies based on your backend because your API should return the total number of records in advance, allowing us to calculate the number of pages and determine when we will reach the last page.&lt;/p&gt;

&lt;p&gt;When we click on the 'Load More' button, the React Query will call the (getNextPageParam) function to retrieve the next page number. It will then pass the page number to the query function (queryFn).&lt;/p&gt;

&lt;p&gt;so (queryFn) we need to add a parameter and destructor it and call (pageParam)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import { useInfiniteQuery } from "@tanstack/react-query";
import axios from "axios";

interface Users {
  id: number;
  name: string;
}

interface UserQuery {
  pageSize: number;
}

const useUsers = (query: UserQuery) =&amp;gt;
  useInfiniteQuery&amp;lt;Users[], Error&amp;gt;({
    queryKey: ["users", query],
// as a best practice we should initialize to 1, 
// so we data for the 
// first page 
    queryFn: ({ pageParam = 1 }) =&amp;gt;
      axios
        .get("https://jsonplaceholder.typicode.com/users", {
          params: {
            _page: pageParam,
            _limit: 4,
          },
        })
        .then((res) =&amp;gt; res.data),

    getNextPageParam(lastPage, allPages) {
      return lastPage.length &amp;gt; 0 ? allPages.length + 1 : undefined;
    },
  });

export default useUsers;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So, in users component let's create a button, when clicking, it will fetch to next page.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import useUsers from "./hooks/useUsers";

const Users = () =&amp;gt; {
  const pageSize = 4;
  const { data, error, isLoading } = useUsers({ pageSize });

  if (isLoading) return &amp;lt;div&amp;gt;Loading.....!&amp;lt;/div&amp;gt;;
  if (error) return &amp;lt;div&amp;gt;{error.message}&amp;lt;/div&amp;gt;;

  return (
    &amp;lt;div&amp;gt;

      &amp;lt;button&amp;gt;Load More&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Users;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;infiniteQuery has a function called fetchNextPage.&lt;/p&gt;

&lt;p&gt;So when you click the button call fetchNextPage.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import useUsers from "./hooks/useUsers";

const Users = () =&amp;gt; {
  const pageSize = 4;
  const { data, error, isLoading, fetchNextPage } = useUsers({ pageSize });

  if (isLoading) return &amp;lt;div&amp;gt;Loading.....!&amp;lt;/div&amp;gt;;
  if (error) return &amp;lt;div&amp;gt;{error.message}&amp;lt;/div&amp;gt;;

  return (
    &amp;lt;div&amp;gt;

      &amp;lt;button onClick={() =&amp;gt; fetchNextPage()}&amp;gt;Load More&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Users;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, we want to disable this button, while we are fetching next page.&lt;/p&gt;

&lt;p&gt;infiniteQuery has a boolean property called isFetchingNextPage.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import useUsers from "./hooks/useUsers";

const Users = () =&amp;gt; {
  const pageSize = 4;
  const { data, error, isLoading, fetchNextPage, isFetchingNextPage } =
    useUsers({ pageSize });

  if (isLoading) return &amp;lt;div&amp;gt;Loading.....!&amp;lt;/div&amp;gt;;
  if (error) return &amp;lt;div&amp;gt;{error.message}&amp;lt;/div&amp;gt;;

  return (
    &amp;lt;div&amp;gt;


      &amp;lt;button disabled={isFetchingNextPage} onClick={() =&amp;gt; fetchNextPage()}&amp;gt;
        Load More
      &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Users;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffgbtrd4kdwy3suoijyiy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffgbtrd4kdwy3suoijyiy.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
data object that we get from the infinite query is an instance of data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fabuel5sv3kucymr3kdgj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fabuel5sv3kucymr3kdgj.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
In this object we have couple of properties &lt;br&gt;
1.) pageParams&lt;br&gt;
2.) pages -&amp;gt; contains data for (allPages).&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import useUsers from "./hooks/useUsers";

const Users = () =&amp;gt; {
  const pageSize = 4;
  const { data, error, isLoading, fetchNextPage, isFetchingNextPage } =
    useUsers({ pageSize });

  if (isLoading) return &amp;lt;div&amp;gt;Loading.....!&amp;lt;/div&amp;gt;;
  if (error) return &amp;lt;div&amp;gt;{error.message}&amp;lt;/div&amp;gt;;

  return (
    &amp;lt;div&amp;gt;
      {data.pages.map((page, index) =&amp;gt; (
        &amp;lt;div key={index}&amp;gt;
          {page.map((user) =&amp;gt; (
            &amp;lt;li&amp;gt;{user.name}&amp;lt;/li&amp;gt;
          ))}
        &amp;lt;/div&amp;gt;
      ))}

      &amp;lt;button disabled={isFetchingNextPage} onClick={() =&amp;gt; fetchNextPage()}&amp;gt;
        Load More
      &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Users;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Thank you!&lt;br&gt;
God bless!&lt;/p&gt;

</description>
      <category>react</category>
      <category>frontend</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What is React-Query and How to fetch data using react query?</title>
      <dc:creator>dravidjones28</dc:creator>
      <pubDate>Tue, 18 Jul 2023 06:24:53 +0000</pubDate>
      <link>https://dev.to/dravidjones28/what-is-react-query-54ih</link>
      <guid>https://dev.to/dravidjones28/what-is-react-query-54ih</guid>
      <description>&lt;p&gt;Normally this is how we fetch data from the backend.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from "axios";
import { useEffect, useState } from "react";

interface Users {
  id: number;
  name: string;
}

const Users = () =&amp;gt; {
  const [users, setUsers] = useState&amp;lt;Users[]&amp;gt;([]);
  const [error, setError] = useState("");

  useEffect(() =&amp;gt; {
    axios
      .get("https://jsonplaceholder.typicode.com/users")
      .then((res) =&amp;gt; setUsers(res.data))
      .catch((err) =&amp;gt; setError(err.message));
  }, []);

  if (error) return &amp;lt;div&amp;gt;{error}&amp;lt;/div&amp;gt;;

  return (
    &amp;lt;div&amp;gt;
      {users.map((item) =&amp;gt; (
        &amp;lt;li key={item.id}&amp;gt;{item.name}&amp;lt;/li&amp;gt;
      ))}
    &amp;lt;/div&amp;gt;
  );
};

export default Users;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we use useState it allows you to add state to functional components and we use useEffect to fetch data from the backend and store in our state variable.&lt;/p&gt;

&lt;p&gt;But there are few problems in this code.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; No request cancellation&lt;/li&gt;
&lt;li&gt;No separation of concerns&lt;/li&gt;
&lt;li&gt;No retries&lt;/li&gt;
&lt;li&gt;No automatic refresh&lt;/li&gt;
&lt;li&gt;No caching&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;1.) No request cancellation: we are not cancelling the request if our component is unmounted.&lt;/p&gt;

&lt;p&gt;2.) No separation of concerns: the query logic is leaked inside the component, we can't reuse it anywhere.&lt;/p&gt;

&lt;p&gt;3.) No retries : we are not retrying failed request.&lt;/p&gt;

&lt;p&gt;4.) No automatic refresh: If the data changes, while the users is on this page, they don't see the changes unless they refresh.&lt;/p&gt;

&lt;p&gt;5.) No caching: if you want to know what is caching? Caching is the process of storing data in a place where it can be accessed more quickly and efficiently in the future.&lt;/p&gt;

&lt;p&gt;We have lots of problem, that's why we use React-query.&lt;/p&gt;

&lt;p&gt;React Query is a powerful library for managing data fetching and caching in React applications.&lt;/p&gt;

&lt;p&gt;To install react query &lt;br&gt;
On your terminal&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;npm i @tanstack/react-query&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import ReactDOM from "react-dom/client";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import App from "./App.tsx";

// We use QueryClient for managing and caching remote data in react query.

// first you need to create a new instance of Query client.
const queryClient = new QueryClient();

ReactDOM.createRoot(document.getElementById("root")!).render(
  &amp;lt;React.StrictMode&amp;gt;
    &amp;lt;QueryClientProvider client={queryClient}&amp;gt;
      &amp;lt;App /&amp;gt;
    &amp;lt;/QueryClientProvider&amp;gt;
  &amp;lt;/React.StrictMode&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  To fetch data with query hook
&lt;/h2&gt;

&lt;p&gt;we use query hook to fetch the data and give configuration object with two properties&lt;br&gt;
1.) queryKey&lt;br&gt;
2.) queryFn&lt;/p&gt;

&lt;p&gt;queryKey -&amp;gt; queryKey is the unique identifier for query, it used for caching, we set this in array of one or more values.&lt;/p&gt;

&lt;p&gt;queryFn -&amp;gt; This is the function we use to fetch the data from the backend, this function should return a promise that resolves data or throws an error.&lt;/p&gt;

&lt;p&gt;In the body the function I have used axios to fetch the data from the backend.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const userQuery = useQuery({
queryKey: ["users"],
queryFn: axios.get("https://jsonplaceholder.typicode.com/users").then(res =&amp;gt; res.data)
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This userQuery has bunch of properties like data, error, isLoading and so on.&lt;/p&gt;

&lt;p&gt;let us destructure the userQuery.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const {data, error, isLoading} = useQuery({
queryKey: ["users"],
queryFn: axios.get("https://jsonplaceholder.typicode.com/users").then(res =&amp;gt; res.data)
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So when calling the query hook, we need to specify the type of errors that might happen when fetching data&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const {data, error, isLoading} = useQuery&amp;lt;User[], Error&amp;gt;({
queryKey: ["users"],
queryFn: axios.get("https://jsonplaceholder.typicode.com/users").then(res =&amp;gt; res.data)
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useQuery } from "@tanstack/react-query";
import axios from "axios";
import { useEffect, useState } from "react";

interface Users {
  id: number;
  name: string;
}

const Users = () =&amp;gt; {
  const { data, error, isLoading } = useQuery&amp;lt;User[], Error&amp;gt;({
    queryKey: ["users"],
    queryFn: () =&amp;gt;
      axios
        .get&amp;lt;Users[]&amp;gt;("https://jsonplaceholder.typicode.com/users")
        .then((res) =&amp;gt; res.data),
  });

if(isLoading) return &amp;lt;div&amp;gt;Loading....!&amp;lt;/div&amp;gt;
if(error) return &amp;lt;div&amp;gt;{error.message}&amp;lt;/div&amp;gt;

  return (
    &amp;lt;div&amp;gt;
      {data?.map((item) =&amp;gt; (
        &amp;lt;li key={item.id}&amp;gt;{item.name}&amp;lt;/li&amp;gt;
      ))}
    &amp;lt;/div&amp;gt;
  );
};

export default Users;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But there is problem in this code, we don't have separation of concerns, our query is leaked.&lt;/p&gt;

&lt;p&gt;so create a folder called hooks -&amp;gt;  create a file called useUsers.ts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useQuery } from "@tanstack/react-query";
import axios from "axios";

interface Users {
  id: number;
  name: string;
}
const useUsers = () =&amp;gt;
  useQuery&amp;lt;Users[], Error&amp;gt;({
    queryKey: ["users"],
    queryFn: () =&amp;gt;
      axios
        .get&amp;lt;Users[]&amp;gt;("https://jsonplaceholder.typicode.com/users")
        .then((res) =&amp;gt; res.data),
  });

export default useUsers;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import useUsers from "./hooks/useUsers";

const Users = () =&amp;gt; {
  const { data, error, isLoading } = useUsers();

  if (isLoading) return &amp;lt;div&amp;gt;Loading.....!&amp;lt;/div&amp;gt;;
  if (error) return &amp;lt;div&amp;gt;{error.message}&amp;lt;/div&amp;gt;;

  return (
    &amp;lt;div&amp;gt;
      {data?.map((item) =&amp;gt; (
        &amp;lt;li key={item.id}&amp;gt;{item.name}&amp;lt;/li&amp;gt;
      ))}
    &amp;lt;/div&amp;gt;
  );
};

export default Users;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thank you!&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactquery</category>
      <category>typescript</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
