Skip to content

Commit 0478e80

Browse files
christopherthielenmergify[bot]
authored andcommitted
refactor(hooks): Replace useViewContextState with useParentView
1 parent 3b13c4d commit 0478e80

File tree

4 files changed

+24
-31
lines changed

4 files changed

+24
-31
lines changed

src/hooks/useIsActive.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
/** @packageDocumentation @reactapi @module react_hooks */
22

3-
import { StateDeclaration } from '@uirouter/core';
43
import { useEffect, useMemo, useState } from 'react';
54
import { UIRouterReact } from '../core';
65
import { useDeepObjectDiff } from './useDeepObjectDiff';
76
import { useOnStateChanged } from './useOnStateChanged';
7+
import { useParentView } from './useParentView';
88
import { useRouter } from './useRouter';
9-
import { useViewContextState } from './useViewContextState';
109

1110
/** @hidden */
12-
function checkIfActive(
13-
router: UIRouterReact,
14-
stateName: string,
15-
params: object,
16-
relative: StateDeclaration,
17-
exact: boolean
18-
) {
11+
function checkIfActive(router: UIRouterReact, stateName: string, params: object, relative: string, exact: boolean) {
1912
return exact
2013
? router.stateService.is(stateName, params, { relative })
2114
: router.stateService.includes(stateName, params, { relative });
@@ -49,7 +42,7 @@ function checkIfActive(
4942
*/
5043
export function useIsActive(stateName: string, params = null, exact = false) {
5144
const router = useRouter();
52-
const relative = useViewContextState(router);
45+
const relative = useParentView().context.name;
5346
// Don't re-compute initialIsActive on every render
5447
const initialIsActive = useMemo(() => checkIfActive(router, stateName, params, relative, exact), []);
5548
const [isActive, setIsActive] = useState(initialIsActive);

src/hooks/useParentView.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/** @packageDocumentation @internalapi @module react_hooks */
2+
3+
import { useContext, useMemo } from 'react';
4+
import { UIViewAddress, UIViewContext } from '../components';
5+
import { useRouter } from './useRouter';
6+
7+
/** Gets the parent UIViewAddress from context, or the root UIViewAddress */
8+
export function useParentView(): UIViewAddress {
9+
const router = useRouter();
10+
const parentUIViewContext: UIViewAddress = useContext(UIViewContext);
11+
return useMemo(() => {
12+
return parentUIViewContext ? parentUIViewContext : { fqn: '', context: router.stateRegistry.root() };
13+
}, [parentUIViewContext, router]);
14+
}

src/hooks/useSref.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { isString, StateDeclaration, TransitionOptions } from '@uirouter/core';
66
import { UISrefActiveContext } from '../components';
77
import { UIRouterReact } from '../core';
88
import { useDeepObjectDiff } from './useDeepObjectDiff';
9+
import { useParentView } from './useParentView';
910
import { useRouter } from './useRouter';
10-
import { useViewContextState } from './useViewContextState';
1111

1212
export interface LinkProps {
1313
onClick: React.MouseEventHandler<any>;
@@ -26,12 +26,12 @@ function useListOfAllStates(router: UIRouterReact) {
2626
}
2727

2828
/** @hidden Gets the StateDeclaration that this sref targets */
29-
function useTargetState(router: UIRouterReact, stateName: string, context: StateDeclaration): StateDeclaration {
29+
function useTargetState(router: UIRouterReact, stateName: string, relative: string): StateDeclaration {
3030
// Whenever any states are added/removed from the registry, get the target state again
3131
const allStates = useListOfAllStates(router);
3232
return useMemo(() => {
33-
return router.stateRegistry.get(stateName, context);
34-
}, [router, stateName, context, allStates]);
33+
return router.stateRegistry.get(stateName, relative);
34+
}, [router, stateName, relative, allStates]);
3535
}
3636

3737
/**
@@ -74,9 +74,9 @@ export function useSref(stateName: string, params: object = {}, options: Transit
7474
// memoize the params object until the nested values actually change so they can be used as deps
7575
const paramsMemo = useMemo(() => params, [useDeepObjectDiff(params)]);
7676

77-
const contextState: StateDeclaration = useViewContextState(router);
78-
const optionsMemo = useMemo(() => ({ relative: contextState, inherit: true, ...options }), [contextState, options]);
79-
const targetState = useTargetState(router, stateName, contextState);
77+
const relative: string = useParentView().context.name;
78+
const optionsMemo = useMemo(() => ({ relative, inherit: true, ...options }), [relative, options]);
79+
const targetState = useTargetState(router, stateName, relative);
8080
// Update href when the target StateDeclaration changes (in case the the state definition itself changes)
8181
// This is necessary to handle things like future states
8282
const href = useMemo(() => {

src/hooks/useViewContextState.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)