Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: wrong types in none lazy hooks, updated readme #28

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@airstack/airstack-react",
"version": "0.4.8",
"version": "0.4.9",
"type": "module",
"scripts": {
"dev": "vite",
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
Loading