-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admin-ui): Add useRouteParams react hook
- Loading branch information
1 parent
7c1454d
commit b63fb7f
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/admin-ui/src/lib/react/src/react-hooks/use-route-params.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { ActivatedRoute } from '@angular/router'; | ||
import { useEffect, useState } from 'react'; | ||
import { useInjector } from './use-injector'; | ||
|
||
/** | ||
* @description | ||
* Provides access to the current route params and query params. | ||
* | ||
* @example | ||
* ```ts | ||
* import { useRouteParams } from '\@vendure/admin-ui/react'; | ||
* import React from 'react'; | ||
* | ||
* export function MyComponent() { | ||
* const { params, queryParams } = useRouteParams(); | ||
* // ... | ||
* return <div>{ params.id }</div>; | ||
* } | ||
* ``` | ||
* | ||
* @docsCategory react-hooks | ||
*/ | ||
export function useRouteParams() { | ||
const activatedRoute = useInjector(ActivatedRoute); | ||
const [params, setParams] = useState(activatedRoute.snapshot.params); | ||
const [queryParams, setQueryParams] = useState(activatedRoute.snapshot.queryParams); | ||
|
||
useEffect(() => { | ||
const subscription = activatedRoute.params.subscribe(value => { | ||
setParams(value); | ||
}); | ||
subscription.add(activatedRoute.queryParams.subscribe(value => setQueryParams(value))); | ||
return () => subscription.unsubscribe(); | ||
}, []); | ||
|
||
activatedRoute; | ||
|
||
return { | ||
params, | ||
queryParams, | ||
}; | ||
} |