Skip to content

Commit

Permalink
Add reset to useDeferredQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
bloodyowl committed Apr 5, 2024
1 parent a39bf41 commit 5cede12
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/react/useDeferredQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export type DeferredQueryConfig = {

export type DeferredQuery<Data, Variables> = readonly [
AsyncData<Result<Data, ClientError>>,
(variables: Variables) => Future<Result<Data, ClientError>>,
{
query: (variables: Variables) => Future<Result<Data, ClientError>>;
reset: () => void;
},
];

export const useDeferredQuery = <Data, Variables>(
Expand Down Expand Up @@ -98,7 +101,12 @@ export const useDeferredQuery = <Data, Variables>(
[runQuery, debounce],
);

const reset = useCallback(() => {
setIsQuerying(false);
setStableVariables(Option.None());
}, []);

const asyncDataToExpose = isQuerying ? AsyncData.Loading() : asyncData;

return [asyncDataToExpose, exposedRunQuery];
return [asyncDataToExpose, { query: exposedRunQuery, reset }];
};

0 comments on commit 5cede12

Please sign in to comment.