Skip to content

Commit 2a32c29

Browse files
committed
add useReloadEffect
1 parent d034f7a commit 2a32c29

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

router/index.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ export const useLoadingEffect = (
5858
useEffect(effect, [useContext(VersionContext), ...deps]);
5959
};
6060

61+
/**
62+
* a hook that runs an effect when the version changes, which is incremented on each route change or reload.
63+
* and skips the first run.
64+
* @param effect the effect to run
65+
* @param deps the dependencies
66+
*/
67+
export const useReloadEffect = (
68+
effect: React.EffectCallback,
69+
deps: React.DependencyList = []
70+
) => {
71+
const [once, setOnce] = useState(true);
72+
useEffect(() => {
73+
if (once) {
74+
setOnce(false);
75+
return;
76+
}
77+
return effect();
78+
}, [useContext(VersionContext), ...deps]);
79+
};
80+
6181
/**
6282
* a context that can be used to reload the current page
6383
*/

0 commit comments

Comments
 (0)