-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Security Solution] Refactor useSelector #75297
[Security Solution] Refactor useSelector #75297
Conversation
Can you please write a summary for this? Wondering the reasoning |
@elasticmachine merge upstream |
.eslintrc.js
Outdated
{ | ||
name: 'react-redux', | ||
importNames: ['useSelector'], | ||
message: 'Please use "useShallowEqualSelector" instead or create your own selector', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you'll still get the error when you create your own selector correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should add a rule here. Many teams work in this code base, for that reason I think we should all use the exact set of lint rules defined by Kibana core.
The Resolver code base, for example, has (AFAIK) no reason to use shallow comparison for selectors as it was designed with the expectation that strict comparison would be used.
import { APP_ID } from '../../../../common/constants'; | ||
import { SecurityPageName } from '../../../app/types'; | ||
import { useKibana } from '../../../common/lib/kibana'; | ||
import { getCaseDetailsUrl, getCreateCaseUrl } from '../../../common/components/link_to'; | ||
import { State } from '../../../common/store'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that we don't have to import State
everywhere now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks clean, performed manual testing around timeline. Couldn't find resolver data to test w today, but I'm confident from timeline testing to LGTM 🏁 🇵🇱
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am opposed to adding a lint rule.
.eslintrc.js
Outdated
{ | ||
name: 'react-redux', | ||
importNames: ['useSelector'], | ||
message: 'Please use "useShallowEqualSelector" instead or create your own selector', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should add a rule here. Many teams work in this code base, for that reason I think we should all use the exact set of lint rules defined by Kibana core.
The Resolver code base, for example, has (AFAIK) no reason to use shallow comparison for selectors as it was designed with the expectation that strict comparison would be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you please provide a change-by-change justification for the changes in Resolver. Resolver was designed with strict comparison of selectors in mind. We don't have plans to adopt the useShallowEqualSelector
pattern.
@@ -51,7 +52,7 @@ export const ProcessDetails = memo(function ProcessDetails({ | |||
}) { | |||
const processName = event.eventName(processEvent); | |||
const entityId = event.entityId(processEvent); | |||
const isProcessTerminated = useSelector(selectors.isProcessTerminated)(entityId); | |||
const isProcessTerminated = useShallowEqualSelector(selectors.isProcessTerminated)(entityId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated to this PR but: isProcessTerminated
actually should be a parametric selector in order to work as expected.
const relatedEventsForThisProcess = useSelector(selectors.relatedEventsByEntityId).get( | ||
processEntityId! | ||
); | ||
const relatedEventsForThisProcess = useShallowEqualSelector( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also unrelated to this PR, but I'm noticing that in some cases we return a memoized thunk from a selector but we actually expect the return value of the thunk to trigger a rerender. In these cases we should consider a parametric selector (still using strict comparison, as all Resolver selectors return primitives or memoized values.)
Suggestion on this PR:
|
@oatkiller you're right, I've reverted all changes related to |
@patrykkopycinski Thank you very much. Your PR brought to our attention some potential bugs in Resolver, I thank you for that. Also, we will keep in mind the hook you added and consider using it in the future. |
@elasticmachine merge upstream |
@elasticmachine merge upstream |
@elasticmachine merge upstream |
…r-use-selector # Conflicts: # x-pack/plugins/security_solution/public/timelines/components/timeline/body/actions/index.test.tsx # x-pack/plugins/security_solution/public/timelines/components/timeline/body/actions/index.tsx # x-pack/plugins/security_solution/public/timelines/components/timeline/body/events/stateful_event.tsx # x-pack/plugins/security_solution/public/timelines/components/timeline/insert_timeline_popover/index.tsx # x-pack/plugins/security_solution/public/timelines/components/timeline/properties/use_create_timeline.tsx
💛 Build succeeded, but was flaky
Test FailuresMark one alert in progress when more than one open alerts are selected.Alerts Marking alerts as in-progress Mark one alert in progress when more than one open alerts are selectedStack Trace
Metrics [docs]@kbn/optimizer bundle module count
async chunks size
page load bundle size
History
To update your PR or re-run it, just comment with: |
* master: (226 commits) [Enterprise Search] Added Logic for the Credentials View (elastic#77626) [CSM] Js errors (elastic#77919) Add the @kbn/apm-config-loader package (elastic#77855) [Security Solution] Refactor useSelector (elastic#75297) Implement tagcloud renderer (elastic#77910) [APM] Alerting: Add global option to create all alert types (elastic#78151) [Ingest pipelines] Upload indexed document to test a pipeline (elastic#77939) TypeScript cleanup in visualizations plugin (elastic#78428) Lazy load metric & mardown visualizations (elastic#78391) [Detections][EQL] EQL rule execution in detection engine (elastic#77419) Update tutorial-full-experience.asciidoc (elastic#75836) Update tutorial-define-index.asciidoc (elastic#75754) Add support for runtime field types to mappings editor. (elastic#77420) [Monitoring] Usage collection (elastic#75878) [Docs][Actions] Add docs for Jira and IBM Resilient (elastic#78316) [Security Solution][Resolver] Update @timestamp formatting (elastic#78166) [Security Solution] Fix app layout (elastic#76668) [Security Solution][Resolver] 2 new functions to DAL (elastic#78477) Adds new elasticsearch client to telemetry plugin (elastic#78046) skip flaky suite (elastic#78512) (elastic#78511) (elastic#78510) (elastic#78509) (elastic#78508) (elastic#78507) (elastic#78506) (elastic#78505) (elastic#78504) (elastic#78503) (elastic#78502) (elastic#78501) (elastic#78500) ...
Summary
https://react-redux.js.org/api/hooks#equality-comparisons-and-updates
TL;DR: To keep the backward-compatibility with
connect()
while moving towards hooks we should use shallow comparison instead of theuseSelector
default, strict reference comparison.Checklist