Skip to content

Commit 25f5958

Browse files
committed
adding usEffect
1 parent 43bc0b5 commit 25f5958

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

x-pack/plugins/apm/public/context/breadcrumbs/use_breadcrumb.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { useCurrentRoute } from '@kbn/typed-react-router-config';
9-
import { useContext, useRef } from 'react';
9+
import { useContext, useRef, useEffect } from 'react';
1010
import { castArray } from 'lodash';
1111
import { Breadcrumb, BreadcrumbsContext } from './context';
1212

@@ -21,19 +21,25 @@ export function useBreadcrumb(breadcrumb: Breadcrumb | Breadcrumb[]) {
2121

2222
const matchedRoute = useRef(match?.route);
2323

24-
if (matchedRoute.current && matchedRoute.current !== match?.route) {
25-
api.unset(matchedRoute.current);
26-
}
27-
28-
matchedRoute.current = match?.route;
29-
30-
if (matchedRoute.current) {
31-
api.set(matchedRoute.current, castArray(breadcrumb));
32-
}
33-
34-
return () => {
35-
if (matchedRoute.current) {
36-
api.unset(matchedRoute.current);
37-
}
38-
};
24+
useEffect(
25+
() => {
26+
if (matchedRoute.current && matchedRoute.current !== match?.route) {
27+
api.unset(matchedRoute.current);
28+
}
29+
30+
matchedRoute.current = match?.route;
31+
32+
if (matchedRoute.current) {
33+
api.set(matchedRoute.current, castArray(breadcrumb));
34+
}
35+
36+
return () => {
37+
if (matchedRoute.current) {
38+
api.unset(matchedRoute.current);
39+
}
40+
};
41+
},
42+
// eslint-disable-next-line react-hooks/exhaustive-deps
43+
[match]
44+
);
3945
}

0 commit comments

Comments
 (0)