React Native provides the Fetch API for your networking needs. Fetch will seem familiar if you have used XMLHttpRequest or other networking APIs before. You may refer to MDN's guide on Using Fetch for additional information.
That means all solutions you already have the chance to use in your reactjs App will work the same in mobile.
React Query is often described as the missing data-fetching library for React, but in more technical terms, it makes fetching, caching, synchronizing and updating server state in your React applications a breeze.
Those some features we will benefits from by default using react-query
:
Install react-query
yarn add react-query
This example very briefly illustrates How to use React Query:
import * as React from "react";
import { QueryClient, QueryClientProvider, useQuery } from "react-query";
import { View, Text } from "ui";
// Create a client
const queryClient = new QueryClient();
export default function App() {
return (
// Provide the client to your App
<QueryClientProvider client={queryClient}>
<Example />
</QueryClientProvider>
);
}
function Example() {
// Queries
const { isLoading, error, data } = useQuery("repoData", () =>
fetch("https://api.github.com/repos/yjose/reactjs-popup").then((res) =>
res.json()
)
);
if (isLoading) return <Text> Loading... </Text>;
if (error) return <Text> An error has occurred: + {error.message}</Text>;
return (
<View flex={1} justifyContent="center" alignItems="center">
<Text>{data.name}</Text>
<Text>{data.description}</Text>
<Text>👀 {data.subscribers_count}</Text>
<Text>✨ {data.stargazers_count}</Text>
<Text>🍴 {data.forks_count}</Text>
</View>
);
}
Create a new folder src/api
to create a global provider and our client instance:
//src/client.tsx
import axios from "axios";
const BASE_URL = "";
export const client = axios.create({
baseURL: BASE_URL,
});
Create a APIProvider and Make to add it to your root tree App.tsx
//src/APIProvider.tsx
import React from "react";
import { QueryClient, QueryClientProvider } from "react-query";
// Create a client
const queryClient = new QueryClient();
type Props = {
children: React.ReactNode;
};
export const APIProvider = ({ children }: Props) => {
return (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);
};
Now you are ready to start creating Quires and Mutation:
// Query
//api/useTasks.tsx
import { useQuery } from "react-query";
import { client } from "./client";
type TaskType = {
label: string;
done: boolean;
color: string;
};
const getTasks = async () => {
const { data } = await client.get("/task");
return data;
};
export function useMe() {
return useQuery<TaskType[]>("tasks", getTasks);
}
// Mutation
//api/useAddTask.tsx
import { useMutation } from "react-query";
import { client } from "./client";
type TaskType = {
label: string,
done: boolean,
color: string,
};
export function useAddTask() {
return useMutation((data: TaskType) => client.post("/task", data));
}
Update Home
Screen to use Data from API
https://60520252fb49dc00175b74f7.mockapi.io
/task
/task/:id
/task
/task/:id
/task/:id
👉 https://github.com/yjose/Tasker/commit/7b06bca11e85e48a1a397787ae31dc251ba3e2ac