Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

Commit

Permalink
fix: dev server refreshing 2 times on first load
Browse files Browse the repository at this point in the history
  • Loading branch information
ytkimirti committed May 15, 2024
1 parent 0f7c270 commit 62fa9a6
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ type Props = {
};

const useRefreshOnDelete = ({ dataKey, refresh }: { dataKey?: string; refresh: () => void }) => {
const firstTime = useRef(true);
const firstRefresh = useRef<number | undefined>();

useEffect(() => {
if (firstTime.current) {
firstTime.current = false;
if (!firstRefresh.current) {
firstRefresh.current = Date.now();
return;
}

// If this is the second call and the time difference is less than 10ms, don't refresh
// This is to prevent react dev server from calling the hook 2 times initially
if (Date.now() - firstRefresh.current < 10) return;

if (dataKey === undefined) {
refresh();
}
Expand Down

0 comments on commit 62fa9a6

Please sign in to comment.