mirror of
https://github.com/elastic/kibana.git
synced 2025-04-19 15:35:00 -04:00
[Security Solution] Fix redux action being fired because of unused react-router value (#217055)
## Summary This pr fixes a bug with the RouteCapture component, used at a high level in the security solution component tree, to reflect url changes into redux. The code previously used the full result of 'react-router-dom' 's useLocation hook as the payload, which contains 4 parameters, pathname, search, hash that we make use of, and a 4th that was added sometime later by the library that is essentially a random id generated every time the hook is called, called key. We have never used this, and it was being inadvertently copied into the redux state, and also causing some other actions or hooks based listeners to run I think as well. Below is the contrived example of going from the home page to an empty alerts page, and you can see 4 actions in the after, and 5 in the before, with 1 updating only the key. May reduce more unneeded actions with more going on in the page, but exactly how many is not known. Before:  After:  ### Checklist - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
This commit is contained in:
parent
f00f83715c
commit
c277812ffe
2 changed files with 9 additions and 7 deletions
|
@ -50,7 +50,6 @@ export interface AppLocation {
|
|||
pathname: string;
|
||||
search: string;
|
||||
hash: string;
|
||||
key?: string;
|
||||
state?:
|
||||
| {
|
||||
isTabChange?: boolean;
|
||||
|
|
|
@ -6,11 +6,10 @@
|
|||
*/
|
||||
|
||||
import type { PropsWithChildren } from 'react';
|
||||
import React, { memo, useEffect } from 'react';
|
||||
import React, { memo, useEffect, useMemo } from 'react';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
import { useDispatch } from 'react-redux';
|
||||
import { TimelineId } from '../../../../common/types';
|
||||
import type { AppLocation } from '../../../../common/endpoint/types';
|
||||
import { timelineActions } from '../../../timelines/store';
|
||||
|
||||
/**
|
||||
|
@ -18,16 +17,20 @@ import { timelineActions } from '../../../timelines/store';
|
|||
* It dispatches actions when the URL is changed.
|
||||
*/
|
||||
export const RouteCapture = memo<PropsWithChildren<unknown>>(({ children }) => {
|
||||
const location: AppLocation = useLocation();
|
||||
const { pathname, search, hash, state } = useLocation();
|
||||
const dispatch = useDispatch();
|
||||
const relevantUrlParams = useMemo(
|
||||
() => ({ pathname, search, hash, state }),
|
||||
[pathname, search, hash, state]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
dispatch(timelineActions.showTimeline({ id: TimelineId.active, show: false }));
|
||||
}, [dispatch, location.pathname]);
|
||||
}, [dispatch, relevantUrlParams.pathname]);
|
||||
|
||||
useEffect(() => {
|
||||
dispatch({ type: 'userChangedUrl', payload: location });
|
||||
});
|
||||
dispatch({ type: 'userChangedUrl', payload: relevantUrlParams });
|
||||
}, [dispatch, relevantUrlParams]);
|
||||
|
||||
return <>{children}</>;
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue