1.0.0 • Published 9 months ago

react-query-callbacks v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

micro library to return @tanstack/react-query callbacks

codecov CI workflow npm

Available callbacks

  • onSettled(data, error) - called when the query status changes to success or error
  • onSuccess(data) - called when the query status changes to success
  • onError(error) - called when the query status changes to error
  • onDataChanged(data) - called every time the data is fetched and is defined

Install

npm install "react-query-callbacks"

Usage

import { useQuery } from "@tanstack/react-query";
import { useQueryCallbacks } from "react-query-callbacks";
import User from "./your-components/User";
import { getUsers } from "./your-services/users";

const Users = () => {
  const usersQuery = useQuery({
    queryKey: ["users"],
    queryFn: getUsers,
  });
  useQueryCallbacks({
    query: usersQuery,
    onError: (error) => {
      console.error(`Could not fetch users: ${error}`);
    },
  });

  return (
    <>
      {(usersQuery.data ?? []).map((user) => (
        <User key={user.id} user={user} />
      ))}
    </>
  );
};

export default Users;