useLocationByRouteContext
useLocationByRouteContext is a React hook that exposes the current Business context and scoped HTTP clients based on the :webAddress route parameter. If the parameter is present, the hook resolves the matching business and returns an httpClient bound to that business. If it’s absent, it falls back to the host-level location context.
It’s designed for multi-location portals where you can deep-link to a specific business (e.g. /london/bookings, /new-york/events).
What you get
The hook returns the following shape:Id: number– The current business IDWebAddress: string– The current business web addresshttpClient: HttpClient– An Axios-like client scoped to the current business (https://{WebAddress}.spaces.Néxudus.com)apiClient: HttpClient– An API root client (baseapiBaseUrlfrom app config)business: Business– The full business record fetched fromendpoints.system.business
Provider and placement
This hook must be used insideLocationByRouteProvider (and within a <Suspense> boundary, because data loading uses Suspense):
LocationContext wrapper, you’re already covered.
How it works
- Reads
:webAddressfrom React Router (useParams()). Routes define it as/:webAddressor nested via:webAddress/*. - If present, fetches
GET /api/sys/businesses/getByHost?host={webAddress}using the rootapiClient. - Builds a business-scoped
httpClient = ApiHttpClient(WebAddress)and copies auth headers from the host context (httpClient.defaults.headers = hostContext.httpClient.defaults.headers) to keep the user session. - Fetches the full
businessviaendpoints.system.businessusing the scopedhttpClient. - Uses
useSuspenseFetch(..., { includeHeaders: false })so auth header changes don’t bust the cache. - If
:webAddressis missing, it falls back to the host-level location fromuseLocationByHostContext.
src/states/useLocationByRouteContext.tsx (provider and hook) and src/states/LocationContext.tsx (composition).
Usage examples
Make business-scoped API calls
Deep-link between businesses
Ensure your routes include:webAddress and build links with it. When a user navigates, the hook will re-resolve the business and swap the scoped httpClient for you.
Routing contract
- Define routes with
:webAddress, for example:path: '/:webAddress'- or nested:
<Route path=":webAddress/*" element={<App />} />
- When the param is present, the context will re-scope to that business; otherwise it uses the host context.
src/routes/index.tsx and src/routes/HomeRouter.tsx.
Edge cases and errors
- Hook usage outside its provider throws:
useLocationByRouteContext must be used within an LocationByRouteContext. - Data loading is Suspense-based; components must render under a
<Suspense>boundary. - If a
:webAddressdoes not resolve to any business, the underlying fetch may trigger your Suspense fallback or error boundary depending on the response. Consider wrapping route segments with an error boundary where appropriate. - Authentication headers are mirrored from the host context each render so sessions persist across business switches.
See also
- useLocationByHostContext – Host-level location resolution and bootstrapping (
src/states/useLocationDomainContext.tsx). - LocationContext – The app wrapper that composes all providers (
src/states/LocationContext.tsx).