Skip to content

Commit c2e2ac1

Browse files
committed
reduce internal states in useBlocker
1 parent 499af9a commit c2e2ac1

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
reduce number of internal state in useBlocker

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
- emzoumpo
6464
- engpetermwangi
6565
- ericschn
66+
- fernandojbf
6667
- FilipJirsak
6768
- frontsideair
6869
- fyzhu

packages/react-router/lib/hooks.tsx

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -935,10 +935,10 @@ let blockerId = 0;
935935
* cross-origin navigations.
936936
*/
937937
export function useBlocker(shouldBlock: boolean | BlockerFunction): Blocker {
938+
let blockerKey = React.useMemo(() => String(++blockerId), []);
938939
let { router, basename } = useDataRouterContext(DataRouterHook.UseBlocker);
939940
let state = useDataRouterState(DataRouterStateHook.UseBlocker);
940941

941-
let [blockerKey, setBlockerKey] = React.useState("");
942942
let blockerFunction = React.useCallback<BlockerFunction>(
943943
(arg) => {
944944
if (typeof shouldBlock !== "function") {
@@ -971,29 +971,18 @@ export function useBlocker(shouldBlock: boolean | BlockerFunction): Blocker {
971971
[basename, shouldBlock]
972972
);
973973

974-
// This effect is in charge of blocker key assignment and deletion (which is
975-
// tightly coupled to the key)
974+
// register router blocker using blocker unique key
976975
React.useEffect(() => {
977-
let key = String(++blockerId);
978-
setBlockerKey(key);
979-
return () => router.deleteBlocker(key);
980-
}, [router]);
981-
982-
// This effect handles assigning the blockerFunction. This is to handle
983-
// unstable blocker function identities, and happens only after the prior
984-
// effect so we don't get an orphaned blockerFunction in the router with a
985-
// key of "". Until then we just have the IDLE_BLOCKER.
986-
React.useEffect(() => {
987-
if (blockerKey !== "") {
988-
router.getBlocker(blockerKey, blockerFunction);
989-
}
976+
router.getBlocker(blockerKey, blockerFunction);
990977
}, [router, blockerKey, blockerFunction]);
991978

992-
// Prefer the blocker from `state` not `router.state` since DataRouterContext
993-
// is memoized so this ensures we update on blocker state updates
994-
return blockerKey && state.blockers.has(blockerKey)
995-
? state.blockers.get(blockerKey)!
996-
: IDLE_BLOCKER;
979+
// unregister only on unmount to not loose state
980+
React.useEffect(() => {
981+
return () => router.deleteBlocker(blockerKey);
982+
}, [router, blockerKey]);
983+
984+
// If key blocker does not exist should be IDLE
985+
return state.blockers.get(blockerKey) || IDLE_BLOCKER;
997986
}
998987

999988
/**

0 commit comments

Comments
 (0)