TanStackT
TanStackโ€ข2y agoโ€ข
5 replies
clean-aquamarine

Is this style correct?

Hello ๐Ÿ‘‹

I use this style to "group" and organize my queries:

import { queryOptions } from "@tanstack/react-query";
import { FighterService } from "../api";

export class FighterQueries {
  public static getFighters() {
    return queryOptions({
      queryKey: ["fighters"],
      queryFn: () => FighterService.getFighters(),
    });
  }

  public static getFighter(name: string) {
    return queryOptions({
      queryKey: ["fighter", name],
      queryFn: () => FighterService.getFighter(name),
    });
  }

  public static getFighterNotes(name: string) {
    return queryOptions({
      queryKey: ["fighter-notes", name],
      queryFn: () => FighterService.getFighterNotes(name),
    });
  }
}


function Page() {
  const query = useSuspenseQuery(FighterQueries.getFighters());

  return (
      <DataTable columns={columns} data={query.data} />
  );
}

I'd like to have confirmation that I'm not making a serious mistake by using this kind of style. Does that seem correct to you? (There is no Suspense because router ensure query data before the page load)
Was this page helpful?