MCPcopy
hub / github.com/TanStack/router / useBlocker

Function useBlocker

packages/react-router/src/useBlocker.tsx:151–260  ·  view source on GitHub ↗
(
  opts?: UseBlockerOpts | LegacyBlockerOpts | LegacyBlockerFn,
  condition?: boolean | any,
)

Source from the content-addressed store, hash-verified

149): BlockerResolver
150
151export function useBlocker(
152 opts?: UseBlockerOpts | LegacyBlockerOpts | LegacyBlockerFn,
153 condition?: boolean | any,
154): BlockerResolver | void {
155 const {
156 shouldBlockFn,
157 enableBeforeUnload = true,
158 disabled = false,
159 withResolver = false,
160 } = _resolveBlockerOpts(opts, condition)
161
162 const router = useRouter()
163 const { history } = router
164
165 const [resolver, setResolver] = React.useState<BlockerResolver>({
166 status: 'idle',
167 current: undefined,
168 next: undefined,
169 action: undefined,
170 proceed: undefined,
171 reset: undefined,
172 })
173
174 React.useEffect(() => {
175 const blockerFnComposed = async (blockerFnArgs: BlockerFnArgs) => {
176 function getLocation(
177 location: HistoryLocation,
178 ): AnyShouldBlockFnLocation {
179 const parsedLocation = router.parseLocation(location)
180 const matchedRoutes = router.getMatchedRoutes(parsedLocation.pathname)
181 if (matchedRoutes.foundRoute === undefined) {
182 return {
183 routeId: '__notFound__',
184 fullPath: parsedLocation.pathname,
185 pathname: parsedLocation.pathname,
186 params: matchedRoutes.routeParams,
187 search: router.options.parseSearch(location.search),
188 }
189 }
190
191 return {
192 routeId: matchedRoutes.foundRoute.id,
193 fullPath: matchedRoutes.foundRoute.fullPath,
194 pathname: parsedLocation.pathname,
195 params: matchedRoutes.routeParams,
196 search: router.options.parseSearch(location.search),
197 }
198 }
199
200 const current = getLocation(blockerFnArgs.currentLocation)
201 const next = getLocation(blockerFnArgs.nextLocation)
202
203 if (
204 current.routeId === '__notFound__' &&
205 next.routeId !== '__notFound__'
206 ) {
207 return false
208 }

Callers 10

setupFunction · 0.90
IndexComponentFunction · 0.90
RootComponentFunction · 0.90
Editor1ComponentFunction · 0.90
RouteComponentFunction · 0.90
RouteComponentFunction · 0.90
RootComponentFunction · 0.90
RootComponentFunction · 0.90
BlockFunction · 0.70

Calls 2

useRouterFunction · 0.90
_resolveBlockerOptsFunction · 0.70

Tested by 2

setupFunction · 0.72
IndexComponentFunction · 0.72