Skip to content

matyaszednicek/zed-query

Repository files navigation

Zed Query - React query library

I create the Zed libraries to deepen my understanding of the inner workings of libraries such as react-query or react-hook-form.


Simple typescript library for handling react queries.

Install

npm install zed-query

Usage

import { useQuery } from "zed-query";

type Post = { title: string; likes: number };

const fetchData: () => Promise<{ posts: Post[] }> = async () => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        posts: [
          { title: "Post 1", likes: 20 },
          { title: "Post 2", likes: 3 },
        ],
      });
    }, 2000);
  });
};

function App() {
  const {
    data: posts,
    isFetching,
    status,
    error,
  } = useQuery({
    queryKey: "posts",
    queryFn: fetchData,
    staleTime: 0,
  });

  return (
    <>
      <div className="w-full flex flex-col space-y-4 text-left">
        <p>Posts:</p>
        {isFetching && (
          <p>{status === "pending" ? "Loading..." : "Refetching..."}</p>
        )}
        {error && <p>Error: {error?.message}</p>}
        {posts && <pre>{JSON.stringify(posts, null, 2)}</pre>}
      </div>
    </>
  );
}

export default App;

About

Zednicek Query (Available on NPM) - TS library for react query fetching

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •