1.14.0 • Published 8 months ago

@aspida/react-query v1.14.0

Weekly downloads
159
License
MIT
Repository
github
Last release
8 months ago

@aspida/react-query

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.

1.13.2

9 months ago

1.14.0

8 months ago

1.13.1

9 months ago

1.13.0

9 months ago

1.13.3

9 months ago

1.12.0

1 year ago

1.11.0

2 years ago

1.10.3

2 years ago

1.10.2

2 years ago

1.10.1

2 years ago

1.10.0

2 years ago

1.9.1

2 years ago

1.9.0

2 years ago

1.8.1

2 years ago

1.8.0

2 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.6.3

3 years ago

1.6.2

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago