-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(useIsActive): Support relative state names
- Loading branch information
1 parent
d14075d
commit 311f1f3
Showing
2 changed files
with
37 additions
and
7 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
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 |
---|---|---|
@@ -1,28 +1,39 @@ | ||
import { StateService } from '@uirouter/core'; | ||
import { StateDeclaration } from '@uirouter/core'; | ||
import { useEffect, useMemo, useState } from 'react'; | ||
import { UIRouterReact } from '../core'; | ||
import { useDeepObjectDiff } from './useDeepObjectDiff'; | ||
import { useOnStateChanged } from './useOnStateChanged'; | ||
import { useRouter } from './useRouter'; | ||
import { useViewContextState } from './useViewContextState'; | ||
|
||
function checkIfActive(stateService: StateService, stateName: string, params: object, exact: boolean) { | ||
return exact ? stateService.is(stateName, params) : stateService.includes(stateName, params); | ||
function checkIfActive( | ||
router: UIRouterReact, | ||
stateName: string, | ||
params: object, | ||
relative: StateDeclaration, | ||
exact: boolean | ||
) { | ||
return exact | ||
? router.stateService.is(stateName, params, { relative }) | ||
: router.stateService.includes(stateName, params, { relative }); | ||
} | ||
|
||
export function useIsActive(stateName: string, params = null, exact = false) { | ||
const { stateService } = useRouter(); | ||
const router = useRouter(); | ||
const relative = useViewContextState(router); | ||
// Don't re-compute initialIsActive on every render | ||
const initialIsActive = useMemo(() => checkIfActive(stateService, stateName, params, exact), []); | ||
const initialIsActive = useMemo(() => checkIfActive(router, stateName, params, relative, exact), []); | ||
const [isActive, setIsActive] = useState(initialIsActive); | ||
|
||
const checkIfActiveChanged = () => { | ||
const newIsActive = checkIfActive(stateService, stateName, params, exact); | ||
const newIsActive = checkIfActive(router, stateName, params, relative, exact); | ||
if (newIsActive !== isActive) { | ||
setIsActive(newIsActive); | ||
} | ||
}; | ||
|
||
useOnStateChanged(checkIfActiveChanged); | ||
useEffect(checkIfActiveChanged, [stateService, stateName, useDeepObjectDiff(params), exact]); | ||
useEffect(checkIfActiveChanged, [router, stateName, useDeepObjectDiff(params), exact]); | ||
|
||
return isActive; | ||
} |