66 */
77
88import { useCurrentRoute } from '@kbn/typed-react-router-config' ;
9- import { useContext , useRef } from 'react' ;
9+ import { useContext , useRef , useEffect } from 'react' ;
1010import { castArray } from 'lodash' ;
1111import { 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