@aspida/react-query

中文文档
1.14.0发布于 2023/08/16MITTypeScriptESM + CJS零依赖近 30 天下载 1984

React Query wrapper for aspida

原始使用文档
文档目录6 节
  1. Getting Started
  2. Installation
  3. Make HTTP request from application
  4. Get response body/status/headers
  5. Use with React Query options
  6. License

@aspida/react-query


aspida
React Query wrapper for aspida.


Getting Started

Installation

  • Using npm:

    $ npm install aspida @aspida/react-query @aspida/axios react-query axios
    # $ npm install aspida @aspida/react-query @aspida/fetch react-query
    # $ npm install aspida @aspida/react-query @aspida/node-fetch react-query node-fetch
    
  • Using Yarn:

    $ yarn add aspida @aspida/react-query @aspida/axios react-query axios
    # $ yarn add aspida @aspida/react-query @aspida/fetch react-query
    # $ yarn add aspida @aspida/react-query @aspida/node-fetch react-query node-fetch
    

Make HTTP request from application

src/index.ts

import aspida from "@aspida/axios"; // "@aspida/fetch", "@aspida/node-fetch"
import { useAspidaQuery } from "@aspida/react-query";
import { QueryClient, QueryClientProvider, useMutation, useQueryClient } from "react-query";
import api from "../api/$api";

const client = api(aspida());
const queryClient = new QueryClient();

function App() {
  return (
    // Provide the client to your App
    <QueryClientProvider client={queryClient}>
      <Todos />
    </QueryClientProvider>
  );
}

function postTodo(body: { id: number; title: string }) {
  return client.todos.$post({ body });
}

function Todos() {
  // Access the client
  const queryClient = useQueryClient();

  // Queries
  const query = useAspidaQuery(client.todos, { query: { limit: 10 } });

  // Mutations
  const mutation = useMutation(postTodo, {
    onSuccess: () => {
      // Invalidate and refetch
      queryClient.invalidateQueries(client.todos.$path({ query: { limit: 10 } }));
    },
  });

  return (
    <div>
      <ul>
        {query.data.map(todo => (
          <li key={todo.id}>{todo.title}</li>
        ))}
      </ul>

      <button
        onClick={() => {
          mutation.mutate({
            id: Date.now(),
            title: "Do Laundry",
          });
        }}
      >
        Add Todo
      </button>
    </div>
  );
}

render(<App />, document.getElementById("root"));

Get response body/status/headers

src/index.ts

import aspida from "@aspida/axios"; // "@aspida/fetch", "@aspida/node-fetch"
import { useAspidaQuery } from "@aspida/react-query";
import { QueryClient, QueryClientProvider } from "react-query";
import api from "../api/$api";

const client = api(aspida());
const queryClient = new QueryClient();

function App() {
  return (
    <QueryClientProvider client={queryClient}>
      <Profile />
    </QueryClientProvider>
  );
}

function Profile() {
  const { data, error } = useAspidaQuery(client.user._userId(123), "get", {
    query: { name: "mario" },
  });

  if (error) return <div>failed to load</div>;
  if (!data) return <div>loading...</div>;
  return (
    <>
      <div>Status: {data.status}</div>
      <div>Headers: {JSON.stringify(data.headers)}</div>
      <div>Name: {data.body.name}</div>
    </>
  );
}

render(<App />, document.getElementById("root"));

useAspidaQuery(client.user._userId(123), { query }) is an alias of useAspidaQuery(client.user._userId(123), "$get", { query })

Use with React Query options

src/index.ts

import aspida from "@aspida/axios"; // "@aspida/fetch", "@aspida/node-fetch"
import { useAspidaQuery } from "@aspida/react-query";
import { QueryClient, QueryClientProvider } from "react-query";
import api from "../api/$api";

const client = api(aspida());
const queryClient = new QueryClient();

function App() {
  return (
    <QueryClientProvider client={queryClient}>
      <Profile />
    </QueryClientProvider>
  );
}

function Profile() {
  const { data, error } = useAspidaQuery(client.user._userId(123), {
    query: { name: "mario" },
    refetchOnMount: true,
    initialData: { name: "anonymous" },
  });

  if (error) return <div>failed to load</div>;
  return <div>hello {data.name}!</div>;
}

render(<App />, document.getElementById("root"));

License

@aspida/react-query is licensed under a MIT License.

版本与发布记录

@aspida/react-query 共发布 27 个版本,首次发布于 2021/01/05;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.14.0latest2023/08/163 年前
1.13.32023/08/043 年前
1.13.22023/08/043 年前
1.13.12023/08/043 年前
1.13.02023/08/033 年前
1.12.02023/04/093 年前

关于 @aspida/react-query 的常见问题

@aspida/react-query 是什么?

React Query wrapper for aspida

如何安装 @aspida/react-query?

在项目目录运行 npm install @aspida/react-query,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:aspida(^1.14.0)、react-query()。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aspida/react-query 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aspida/react-query 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@aspida/react-query 可以商用吗?

@aspida/react-query 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@aspida/react-query 的下载量有多少?

根据 npm 官方下载统计,@aspida/react-query 在 2026/10/07 – 2026/11/05 这 30 天内被下载 1,984 次,日均约 66 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aspida/react-query 还在维护吗?

最新版本 1.14.0 发布于 2023/08/16(3 年前);至今共发布 27 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/01/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。