Skip to content

Commit fde4a3d

Browse files
[8.18] [Security Solution] Fix redux action being fired because of unused react-router value (elastic#217055) (elastic#218454)
# Backport This will backport the following commits from `main` to `8.18`: - [[Security Solution] Fix redux action being fired because of unused react-router value (elastic#217055)](elastic#217055) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Kevin Qualters","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-04-16T14:13:44Z","message":"[Security Solution] Fix redux action being fired because of unused react-router value (elastic#217055)\n\n## Summary\n\nThis pr fixes a bug with the RouteCapture component, used at a high\nlevel in the security solution component tree, to reflect url changes\ninto redux. The code previously used the full result of\n'react-router-dom' 's useLocation hook as the payload, which contains 4\nparameters, pathname, search, hash that we make use of, and a 4th that\nwas added sometime later by the library that is essentially a random id\ngenerated every time the hook is called, called key. We have never used\nthis, and it was being inadvertently copied into the redux state, and\nalso causing some other actions or hooks based listeners to run I think\nas well.\n\nBelow is the contrived example of going from the home page to an empty\nalerts page, and you can see 4 actions in the after, and 5 in the\nbefore, with 1 updating only the key. May reduce more unneeded actions\nwith more going on in the page, but exactly how many is not known.\nBefore:\n\n![image](https://github.com/user-attachments/assets/93cc2c5a-56e4-4764-8791-c41879fd5b45)\n\n\nAfter:\n\n![image](https://github.com/user-attachments/assets/ebd75055-4e17-497b-bed2-a5fd58c5c92f)\n\n\n### Checklist\n\n- [ ] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios","sha":"c277812ffe3e74eb9bc0733a538bd78ea9b95f58","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Threat Hunting:Investigations","backport:all-open","v9.1.0"],"title":"[Security Solution] Fix redux action being fired because of unused react-router value","number":217055,"url":"https://github.com/elastic/kibana/pull/217055","mergeCommit":{"message":"[Security Solution] Fix redux action being fired because of unused react-router value (elastic#217055)\n\n## Summary\n\nThis pr fixes a bug with the RouteCapture component, used at a high\nlevel in the security solution component tree, to reflect url changes\ninto redux. The code previously used the full result of\n'react-router-dom' 's useLocation hook as the payload, which contains 4\nparameters, pathname, search, hash that we make use of, and a 4th that\nwas added sometime later by the library that is essentially a random id\ngenerated every time the hook is called, called key. We have never used\nthis, and it was being inadvertently copied into the redux state, and\nalso causing some other actions or hooks based listeners to run I think\nas well.\n\nBelow is the contrived example of going from the home page to an empty\nalerts page, and you can see 4 actions in the after, and 5 in the\nbefore, with 1 updating only the key. May reduce more unneeded actions\nwith more going on in the page, but exactly how many is not known.\nBefore:\n\n![image](https://github.com/user-attachments/assets/93cc2c5a-56e4-4764-8791-c41879fd5b45)\n\n\nAfter:\n\n![image](https://github.com/user-attachments/assets/ebd75055-4e17-497b-bed2-a5fd58c5c92f)\n\n\n### Checklist\n\n- [ ] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios","sha":"c277812ffe3e74eb9bc0733a538bd78ea9b95f58"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/217055","number":217055,"mergeCommit":{"message":"[Security Solution] Fix redux action being fired because of unused react-router value (elastic#217055)\n\n## Summary\n\nThis pr fixes a bug with the RouteCapture component, used at a high\nlevel in the security solution component tree, to reflect url changes\ninto redux. The code previously used the full result of\n'react-router-dom' 's useLocation hook as the payload, which contains 4\nparameters, pathname, search, hash that we make use of, and a 4th that\nwas added sometime later by the library that is essentially a random id\ngenerated every time the hook is called, called key. We have never used\nthis, and it was being inadvertently copied into the redux state, and\nalso causing some other actions or hooks based listeners to run I think\nas well.\n\nBelow is the contrived example of going from the home page to an empty\nalerts page, and you can see 4 actions in the after, and 5 in the\nbefore, with 1 updating only the key. May reduce more unneeded actions\nwith more going on in the page, but exactly how many is not known.\nBefore:\n\n![image](https://github.com/user-attachments/assets/93cc2c5a-56e4-4764-8791-c41879fd5b45)\n\n\nAfter:\n\n![image](https://github.com/user-attachments/assets/ebd75055-4e17-497b-bed2-a5fd58c5c92f)\n\n\n### Checklist\n\n- [ ] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios","sha":"c277812ffe3e74eb9bc0733a538bd78ea9b95f58"}}]}] BACKPORT--> Co-authored-by: Kevin Qualters <[email protected]>
1 parent 16aed64 commit fde4a3d

File tree

2 files changed

+9
-7
lines changed
  • x-pack/solutions/security/plugins/security_solution

2 files changed

+9
-7
lines changed

x-pack/solutions/security/plugins/security_solution/common/endpoint/types/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ export interface AppLocation {
5050
pathname: string;
5151
search: string;
5252
hash: string;
53-
key?: string;
5453
state?:
5554
| {
5655
isTabChange?: boolean;

x-pack/solutions/security/plugins/security_solution/public/common/components/endpoint/route_capture.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,31 @@
66
*/
77

88
import type { PropsWithChildren } from 'react';
9-
import React, { memo, useEffect } from 'react';
9+
import React, { memo, useEffect, useMemo } from 'react';
1010
import { useLocation } from 'react-router-dom';
1111
import { useDispatch } from 'react-redux';
1212
import { TimelineId } from '../../../../common/types';
13-
import type { AppLocation } from '../../../../common/endpoint/types';
1413
import { timelineActions } from '../../../timelines/store';
1514

1615
/**
1716
* This component should be used above all routes, but below the Provider.
1817
* It dispatches actions when the URL is changed.
1918
*/
2019
export const RouteCapture = memo<PropsWithChildren<unknown>>(({ children }) => {
21-
const location: AppLocation = useLocation();
20+
const { pathname, search, hash, state } = useLocation();
2221
const dispatch = useDispatch();
22+
const relevantUrlParams = useMemo(
23+
() => ({ pathname, search, hash, state }),
24+
[pathname, search, hash, state]
25+
);
2326

2427
useEffect(() => {
2528
dispatch(timelineActions.showTimeline({ id: TimelineId.active, show: false }));
26-
}, [dispatch, location.pathname]);
29+
}, [dispatch, relevantUrlParams.pathname]);
2730

2831
useEffect(() => {
29-
dispatch({ type: 'userChangedUrl', payload: location });
30-
});
32+
dispatch({ type: 'userChangedUrl', payload: relevantUrlParams });
33+
}, [dispatch, relevantUrlParams]);
3134

3235
return <>{children}</>;
3336
});

0 commit comments

Comments
 (0)