@@ -935,10 +935,10 @@ let blockerId = 0;
935935 * cross-origin navigations.
936936 */
937937export 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