** This project is not under maintaining ***
https://github.com/tannerlinsley/react-query
A collection of hooks for fetching data easily.
$ yarn add react-concurrent
Get your api data easily by useFetching
import { useFetching } from "react-concurrent";
const app = () => {
const { data, isLoading, error, refetch } = useFetching(() =>
fetch("http://example.com").then((res) => res.json()),
);
};
//////// Axios
// If you are using axios
const { data, isLoading, error, refetch } = useFetching(async () => {
const response = await axios.get("http://example.com");
return response.data;
});
useFetchingCallback doesn't fetching until call refetch
import { useFetchingCallback } from "react-concurrent";
const app = () => {
const { data, isLoading, error, refetch } = useFetchingCallback((body) =>
fetch("http://example.com/setting", { method: "post", body }),
);
return (
<>
<Button onPress={() => refetch({ language: "en" })} title="English" />
{isLoading ? "Is loading ..." : data}
</>
);
};
useFetch give us a resource, we need to pass that to useResource for get data
import { useCreateResource, useResource } from "react-concurrent";
const fetchApi = (id) => fetch(`http://example.com/${id}`);
const app = () => {
const [id, setId] = useState(1); // fetch is calling again if this state changed
const { resource } = useCreateResource(() => fetchApi(id), [id]);
return <OtherComponent {...{ resource }} />;
};
const OtherComponent = ({ resource }) => {
const { data, isLoading, error } = useResource(resource);
};
import { createResource, useResource } from "react-concurrent";
const resource = createResource(() => fetch("http://example.com"));
// resource.preload(); // fetching before render component
const app = () => {
const { data, isLoading, error } = useResource(resource);
};
As mentioned on react document you could use this
import { createResource } from "react-concurrent";
const resource = createResource(() => fetch("http://example.com"));
const App = () => {
return (
<Suspense fallback={"Is loading ...."}>
<OtherComponent />
</Suspense>
);
};
const OtherComponent = () => {
const data = resource.read();
return "loaded";
};