All products

TanStack QueryOfficial

BitaKit plugin with one shared TanStack Query client for every plugin and the React tree.

bitakit · Plugin for every BitaKit app

Type
Package
Needs a build
Kind
Plugin
BitaKit
Version
0.3.2
Latest
Works with
Any app
Every BitaKit app
Versions
2
versions
Publisher
bitakit
Official

Preview

1 picture

TanStack Query

About

@bitakit/query

One shared TanStack Query client for a BitaKit app. Services read and write through it, and React components use the standard useQuery / useMutation hooks on the same cache. Integrations such as @bitakit/strapi cache their reads here.

npm install @bitakit/query @tanstack/react-query

With discovery, installing the package is enough: the plugin registers the query-client service and wraps the React tree in QueryClientProvider. Defaults go in an optional src/configs/query.ts:

// src/configs/query.ts
export default { defaultOptions: { queries: { staleTime: 30_000 } } };

Configuration is plain data, so callbacks such as a retry function belong to the individual queries.

In services and Actions

import { defineService, getService } from '@bitakit/core';
import { queryClient } from '@bitakit/query';

export default defineService(
  ({ services }) => {
    const { client } = getService(services, queryClient);
    return {
      stats: () => client.fetchQuery({ queryKey: ['stats'], queryFn: loadStats }),
    };
  },
  { requires: [queryClient] },
);

In React

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

const { data } = useQuery({ queryKey: ['stats'], queryFn: loadStats }); // same cache as the service

Without discovery

The root entry is headless (no React):

import { createRuntime } from '@bitakit/core';
import { queryPlugin } from '@bitakit/query';

const runtime = createRuntime({ plugins: [queryPlugin({ defaultOptions: {} })] });

Notes

  • The client is created with the plugin, not by the service, so the React provider renders it before the runtime is ready. The service mounts it (focus and online refetching) on start and unmounts it on stop.
  • @tanstack/query-core and @tanstack/react-query are peer dependencies: the app and every plugin share one installed copy, which useQuery needs to find the provider.
  • A plugin that keeps per-user data in the cache removes it when the user changes; the @bitakit/strapi auth service does this for its reads.

Permissions

It adds and checks no permissions.

Information

Publisher
bitakit · Official
Type
PackageNeeds installation: add it to the app's code and build the app.
Kind
Plugin
Works with
Every BitaKit app
Versions
0.3.20.3.1