Skip to content

Commit

Permalink
refactor(react): useRef instead to track prev loader
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Sep 15, 2023
1 parent a9c7307 commit 7ffac41
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/react.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useSelector, useDispatch } from 'react-redux';
import { useState, useEffect, useMemo } from 'react';
import { useState, useEffect, useMemo, useRef } from 'react';
import type { LoadingState } from 'robodux';

import type { QueryState } from './slice';
Expand Down Expand Up @@ -207,15 +207,14 @@ export function useCache<D = any, A extends SagaAction = SagaAction>(
* ```
*/
export function useLoaderSuccess(
cur: Pick<LoadingState, 'isLoading' | 'isSuccess'>,
cur: Pick<LoadingState, 'status'>,
success: () => any,
) {
const [prev, setPrev] = useState(cur);
const prev = useRef(cur);
useEffect(() => {
const curSuccess = !cur.isLoading && cur.isSuccess;
if (prev.isLoading && curSuccess) {
if (prev.current.status !== 'success' && cur.status === 'success') {
success();
}
setPrev(cur);
}, [cur.isSuccess, cur.isLoading]);
prev.current = cur;
}, [cur.status]);
}

0 comments on commit 7ffac41

Please sign in to comment.