Repository navigation
[EuiSearchBar] Migrate from class component to functional component - #9651
Conversation
|
💚 CLA has been signed |
|
👋 Since this is a community submitted pull request, a Buildkite build has not been started automatically. Would an Elastic organization member please verify the contents of this pull request and kick off a build manually? |
There was a problem hiding this comment.
Pull request overview
Migrates EuiSearchBar from a class component to a function component using React hooks, while aiming to preserve existing behavior (query parsing, filter integration, hint popover, and the EuiSearchBar.Query static API).
Changes:
- Replaced class state/lifecycle (
constructor,getDerivedStateFromProps) withuseState+useEffectprop→state syncing. - Swapped
RenderWithEuiThemerender-prop usage for theuseEuiThemehook. - Extracted helper logic (
renderTools,notifyControllingParent) into standalone functions and attempted to preserveEuiSearchBar.Query.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
weronikaolejniczak
left a comment
There was a problem hiding this comment.
I don't think useEffect is the right replacement for getDerivedStateFromProps. That lifecycle method runs synchronously during render before anything is commited. The useEffect runs after render and commit. This means the component would render with stale query, useEffect would run and it would re-render with correct value.
As per React docs, the correct pattern here is "update state during the render phase":
const [prevPropsQuery, setPrevPropsQuery] = useState(props.query);
if (prevPropsQuery !== props.query) {
const nextQuery = props.query;
const shouldUpdate =
(nextQuery || nextQuery === '') &&
(!query || /* ... same conditions ... */);
if (shouldUpdate) {
setPrevPropsQuery(props.query);
const parsed = parseQuery(nextQuery, props);
setQuery(parsed);
setError(null);
}
}|
Applied the suggested pattern. I made a few small adjustments:
|
| setPrevPropsQuery(props.query); | ||
| const parsedQuery = parseQuery(nextQuery, props); | ||
| setQuery(parsedQuery); | ||
| setQueryText(parsedQuery.text); | ||
| setError(null); |
There was a problem hiding this comment.
I don't think this is a real concern. The React docs are clear about that.
What I would be more concerned about, and it's something that you did @Dmc0125 but then removed, is move setPrevPropsQuery outside of the shouldUpdate to avoid them being stale. In practice this shouldn't be a big issue but I'd double-check in Strict mode.
| ); | ||
| }; | ||
|
|
||
| EuiSearchBar.Query = Query; |
There was a problem hiding this comment.
There are no errors emitted so I don't know what this is about 🤷🏻♀️
weronikaolejniczak
left a comment
There was a problem hiding this comment.
I smoke-tested the component and it looks good 👌🏻 Thank you for addressing my concerns, @Dmc0125!
|
Thanks for the thorough review and for the feedback. Learned a lot from this :) |
1 visual difference(s) found - expand to review, then click Approve visual changes to update baselineseuidatagrid (1 difference)
|
|
buildkite test this |
💚 Build Succeeded
History
|
💚 Build Succeeded
History
|



Summary
API Changes
Screenshots
Impact Assessment
Note: Most PRs should be tested in Kibana to help gauge their Impact before merging.
Impact level: 🟢 None
Release Readiness
Documentation:Figma:Migration guide:Adoption planQA instructions for reviewer
Checklist before marking Ready for Review
Reviewer checklist