Skip to content

Commit

Permalink
fix type issue, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
aadilhasan committed Aug 7, 2023
1 parent 2d89819 commit c5d810c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ The `useQuery` hook loads query data as soon as the component is mounted. It ret
import { useQuery } from "@airstack/airstack-react";

const MyComponent = () => {
const { data, loading, error } = useQuery(query, variables);
const { data, loading, error } = useQuery(query, variables, configAndCallbacks);

if (loading) {
return <p>Loading...</p>;
Expand Down Expand Up @@ -186,7 +186,8 @@ import { useQueryWithPagination } from "@airstack/airstack-react";
const MyComponent = () => {
const { data, loading, pagination } = useQueryWithPagination(
query,
variables
variables,
configAndCallbacks
);
const { hasNextPage, hasPrevPage, getNextPage, getPrevPage } = pagination;

Expand Down Expand Up @@ -295,7 +296,8 @@ function App() {

## fetchQuery

fetchQuery can be used in places where using hooks is not possible. `fetchQuery` accepts same parameter as hooks .
fetchQuery can be used in places where using hooks is not possible. `fetchQuery` accepts same parameter as hooks.
**Note**: fetchQuery only accepts config and do not accept callbacks in the third parameter.

`fetchQuery` returns a promise with an object, which contains the following properties:

Expand Down
8 changes: 4 additions & 4 deletions src/lib/hooks/useQuery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useEffect } from "react";
import { fetchQuery } from "../apis/fetchQuery";
import { Config, ConfigAndCallbacks, FetchQueryReturnType, Variables } from "../types";
import { ConfigAndCallbacks, FetchQueryReturnType, Variables } from "../types";
import { useRequestState } from "./useDataState";

type UseQueryReturnType = {
Expand Down Expand Up @@ -29,7 +29,7 @@ export function useLazyQuery(
variablesRef,
setData,
setError,
setLoading
setLoading,
} = useRequestState(variables, configAndCallbacks);

const handleResponse = useCallback(
Expand Down Expand Up @@ -71,12 +71,12 @@ export function useLazyQuery(
export function useQuery(
query: string,
variables?: Variables,
config?: Config
configAndCallbacks?: ConfigAndCallbacks
): UseQueryReturnType {
const [fetch, { data, error, loading }] = useLazyQuery(
query,
variables,
config
configAndCallbacks
);

useEffect(() => {
Expand Down
11 changes: 7 additions & 4 deletions src/lib/hooks/useQueryWithPagination.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useState, useRef, useCallback, useEffect, useMemo } from "react";
import {
Config,
FetchQuery,
FetchPaginatedQueryReturnType,
Variables,
Expand Down Expand Up @@ -49,7 +48,7 @@ export function useLazyQueryWithPagination(
variablesRef,
setData,
setError,
setLoading
setLoading,
} = useRequestState(variables, configAndCallbacks);
const [hasNextPage, setHasNextPage] = useState(false);
const [hasPrevPage, setHasPrevPage] = useState(false);
Expand Down Expand Up @@ -165,9 +164,13 @@ export function useLazyQueryWithPagination(
export function useQueryWithPagination(
query: string,
variables?: Variables,
config?: Config
configAndCallbacks?: ConfigAndCallbacks
): UseQueryReturnType {
const [fetch, data] = useLazyQueryWithPagination(query, variables, config);
const [fetch, data] = useLazyQueryWithPagination(
query,
variables,
configAndCallbacks
);
useEffect(() => {
fetch();
}, [fetch]);
Expand Down

0 comments on commit c5d810c

Please sign in to comment.