-
Notifications
You must be signed in to change notification settings - Fork 36
fix(ebay-tabs): prevent onSelect from firing on initial render #579
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@ebay/ui-core-react": patch | ||
| --- | ||
|
|
||
| prevent onSelect from firing on initial render |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import React, { cloneElement, FC, KeyboardEvent, useEffect, useState } from "react"; | ||
| import React, { cloneElement, FC, KeyboardEvent, useEffect, useRef, useState } from "react"; | ||
| import classNames from "classnames"; | ||
|
|
||
| import { handleActionKeydown, handleLeftRightArrowsKeydown } from "../common/event-utils"; | ||
|
|
@@ -61,8 +61,12 @@ const Tabs: FC<TabsProps> = ({ | |
| }); | ||
| }; | ||
|
|
||
| const prevIndex = useRef(index); | ||
| useEffect(() => { | ||
| handleSelect(index); | ||
| if (prevIndex.current !== index) { | ||
| prevIndex.current = index; | ||
| handleSelect(index); | ||
| } | ||
| }, [index]); | ||
|
Comment on lines
65
to
70
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should remove the
Comment on lines
+64
to
70
|
||
|
|
||
| const isLarge = size === "large"; | ||
|
|
||
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.
The effect still calls
handleSelect(index), which triggersonSelect. This meansonSelectwill fire for any prop-drivenselectedIndexchange after mount (not just user interaction), and it will also double-fire in a controlled pattern where the parent updatesselectedIndexin response toonSelect(click triggersonSelect, then the prop change triggersonSelectagain). Consider syncing internal state from theselectedIndexprop in the effect without callingonSelect(e.g., updateselectedIndexstate directly), soonSelectis reserved for user-initiated selection changes.