Skip to content

Commit

Permalink
Default dependency array for useAsync and useAsyncRetry
Browse files Browse the repository at this point in the history
  • Loading branch information
wardoost committed Mar 31, 2019
1 parent e0916ed commit 2c49244
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/useAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type AsyncState<T> =
value: T;
};

const useAsync = <T>(fn: () => Promise<T>, deps: DependencyList) => {
const useAsync = <T>(fn: () => Promise<T>, deps: DependencyList = []) => {
const [state, set] = useState<AsyncState<T>>({
loading: true
});
Expand Down
7 changes: 5 additions & 2 deletions src/useAsyncRetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import useAsync, { AsyncState } from './useAsync';
export type AsyncStateRetry<T> = AsyncState<T> & {
retry():void
}
const useAsyncRetry = <T>(fn: () => Promise<T>, deps: DependencyList) => {

const useAsyncRetry = <T>(fn: () => Promise<T>, deps: DependencyList = []) => {
const [attempt, setAttempt] = useState<number>(0);
const state = useAsync(fn,[...deps, attempt]);
const state = useAsync(fn, [...deps, attempt]);

const stateLoading = state.loading;
const retry = useCallback(() => {
if (stateLoading) {
if (process.env.NODE_ENV === 'development') {
console.log('You are calling useAsyncRetry hook retry() method while loading in progress, this is a no-op.');
}

return;
}

setAttempt(attempt => attempt + 1);
}, [...deps, stateLoading, attempt]);

Expand Down

0 comments on commit 2c49244

Please sign in to comment.