npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@sayyyat/react-query-conditional

v1.0.2

Published

Declarative conditional component for TanStack Query states (isLoading, isError, isEmpty).

Readme

@sayyyat/react-query-conditional

npm version CI/CD Status npm bundle size License: MIT

A simple, zero-dependency, declarative React component to gracefully handle @tanstack/react-query states (isPending, isError, isEmpty) without repeating logic.


The Problem

When using TanStack Query, you often find yourself repeating the same boilerplate logic in every component:

function MyComponent() {
  const { data, isPending, isError } = useQuery(...);

  if (isPending) {
    return <LoadingSpinner />;
  }

  if (isError) {
    return <ErrorState />;
  }

  if (!data || data.length === 0) {
    return <EmptyState />;
  }

  // Finally, the "happy path"
  return <DisplayData data={data} />;
}

This is repetitive, imperative, and mixes state logic heavily with your component's presentation.

The Solution: <Conditional />

This component encapsulates all that logic, allowing you to write clean, declarative code.

import { Conditional } from '@sayyyat/react-query-conditional';
import { useQuery } from '@tanstack/react-query';

function MyComponent() {
  const query = useQuery(...);

  return (
    <Conditional query={query}>
      {(results) => <DisplayData data={results[0].data} />}
    </Conditional>
  );
}

The component automatically handles isPending, isError, and isEmpty states, rendering its children (as a function) only for the "happy path".

Features

  • Zero-Dependency: Works out-of-the-box with simple default states. It is not dependent on Tailwind or any other styling library.
  • Fully Customizable: Easily override the default skeleton, error, and empty components with your own (e.g., Tailwind + i18n components).
  • Handles Multiple Queries: Can track an array of queries ([queryA, queryB]) and wait for all or any to complete.
  • Render Prop API: Uses the children-as-a-function pattern for a clean, declarative API.

Installation

# pnpm
pnpm add @sayyyat/react-query-conditional

# yarn
yarn add @sayyyat/react-query-conditional

# npm
npm install @sayyyat/react-query-conditional

Usage

Basic Example

The component automatically provides built-in (framework-agnostic) components for skeleton, error, and empty states.

import { Conditional } from '@sayyyat/react-query-conditional';
const query = useQuery(...);

return (
  <Conditional query={query}>
    {/* This is only rendered on success and when data is not empty */}
    {(results) => (
      <div>
        <h1>{results[0].data.title}</h1>
      </div>
    )}
  </Conditional>
);

Customizing States (for Tailwind, i18n, etc.)

You can easily provide your own components for each state. This is the recommended way to use this library if you use Tailwind/MUI or need internationalization (i18n).

import { Conditional } from '@sayyyat/react-query-conditional';
import { MyTailwindSpinner } from '@/components/ui/MySpinner';
import { MyTranslatedError } from '@/components/ui/MyError';
import { MyEmptyState } from '@/components/ui/MyEmptyState';

const query = useQuery(...);

<Conditional
  query={query}
  skeleton={<MyTailwindSpinner />}
  error={<MyTranslatedError message="Деректерді жүктеу кезінде қате орын алды" />}
  empty={<MyEmptyState />}
>
  {(results) => ...}
</Conditional>

Handling Multiple Queries

Use the queries prop to wait for multiple requests. Use mode="all" (default) to wait for all queries to succeed, or mode="any" to render as soon as one succeeds.

const userQuery = useQuery({ queryKey: ['user'], ... });
const postsQuery = useQuery({ queryKey: ['posts'], ... });

return (
  <Conditional 
    queries={[userQuery, postsQuery]} 
    mode="all"
  >
    {(results) => {
      const user = results[0].data;
      const posts = results[1].data;
      return <UserProfile user={user} posts={posts} />;
    }}
  </Conditional>
);

API / Props

| Prop | Type | Default | Description | | :--- | :--- | :--- | :--- | | children | ReactNode \| (results: TQ[]) => ReactNode | Required | The content to render on success, or a function that receives the array of query results. | | query | UseQueryResult | undefined | A single TanStack Query result object. | | queries | UseQueryResult[] | [] | An array of TanStack Query result objects. | | skeleton | ReactNode | <Loading /> | Component to show while any query isPending. | | error | ReactNode \| (errors: unknown[], results: TQ[]) => ReactNode | <ErrorState /> | Component to show if any query isError. Can be a function. | | empty | ReactNode \| (results: TQ[]) => ReactNode | <EmptyState /> | Component to show if data is empty (null, undefined, or empty array). | | treatErrorAsEmpty | boolean | true | If true, renders the empty component on error instead of the error component. Useful for 404s. | | requireData | boolean | true | If true, success requires data to be non-null. If false, isSuccess is enough. | | mode | "all" \| "any" | "all" | For multiple queries: wait for all to succeed or just any to succeed. |

(TQ type is UseQueryResult<any, any>)

License

MIT © Sayat Raykul