-
-
Notifications
You must be signed in to change notification settings - Fork 244
Open
Description
Problem Description
When using rc-tabs, tab click events can fire multiple times unexpectedly, especially when the component re-renders frequently. This happens because the onInternalTabClick function is recreated on every render without proper memoization.
Root Cause
The issue is in the Tabs.js file where onInternalTabClick function is defined as a regular function instead of using React.useCallback. This causes the function to be recreated on every render, which can lead to:
- Unnecessary re-renders of child components
- Multiple event handlers being attached
- Click events firing multiple times
Solution
The fix involves two main changes:
- Memoize tab keys: Use
React.useMemoto memoize the tab keys array to prevent unnecessary re-computations - Memoize click handler: Wrap
onInternalTabClickwithReact.useCallbackto prevent function recreation on every render
Code Changes
// Before
var tabKeys = React.useMemo(function () {
return tabs.map(function (tab) {
return tab.key;
});
}, [tabs]);
// After
var tabKeys = React.useMemo(function () {
return tabs.map(function (tab) {
return tab.key;
});
}, [tabs]);
// Before
function onInternalTabClick(key, e) {
onTabClick === null || onTabClick === void 0 || onTabClick(key, e);
var isActiveChanged = key !== mergedActiveKey;
setMergedActiveKey(key);
if (isActiveChanged) {
onChange === null || onChange === void 0 || onChange(key);
}
}
// After
var onInternalTabClick = React.useCallback(function (key, e) {
onTabClick === null || onTabClick === void 0 || onTabClick(key, e);
var isActiveChanged = key !== mergedActiveKey;
setMergedActiveKey(key);
if (isActiveChanged) {
onChange === null || onChange === void 0 || onChange(key);
}
}, [onTabClick, mergedActiveKey, onChange]);Reproduction Steps
- Create a Tabs component with dynamic content
- Add console.log in the onChange handler
- Click on a tab multiple times
- Observe that the onChange handler fires multiple times for a single click
Expected Behavior
Each tab click should trigger the onChange handler exactly once, regardless of component re-renders.
Environment
- rc-tabs version: [affected versions]
- React version: [tested versions]
- Browser: All browsers
Impact
This issue affects applications that:
- Have frequent re-renders
- Use dynamic tab content
- Rely on accurate click event handling
- Need to prevent duplicate API calls or state updates
The fix improves performance and prevents unexpected behavior in applications using rc-tabs.
Metadata
Metadata
Assignees
Labels
No labels