[tabs] Wrong tab selected when tab array changes
See original GitHub issueš Bug report
Current Behavior
Iām working an application where we use @reach/tabs to manage our tabs. The list of tabs can changeāboth in length and in the ordering of tabsāwhen the user is using the app. Weāre using the tabs in a controlled way, i.e. we provide an index
and onChange
to the Tabs component.
Iām currently seeing three issues:
- When I add a new tab to the end of tabs array and set the
index
to the last index in the array, the second-to-last tab is selected. When I add more tabs, the selected tab doesnāt change. It seems to be set to the last element of the original array, before any additions. - When I change the order of the tabs and keep the index constant, the tab thatās selected is always the one that was selected before the order change instead of the one specified by the index.
- When I change the order of the tabs and try to keep the same tab selected as before, a random (?) tab is selected.
If I inspect the components, it seems that the right Tab component is receiving the isSelected = true
prop, but the CSS styles are incorrect. If I unmount the component and remount it, the correct tab is shown.
Is this a bug or am I using the library incorrectly? Might this be caused by some sort of tab array caching and/or the caching of the CSS styles?
Expected behavior
I would expect the index
number to select the right tab.
Reproducible example
Suggested solution(s)
N/A
Additional context
N/A
Your environment
Software | Name(s) | Version |
---|---|---|
Reach Package | @reach/tabs | 0.16.1 |
React | react | 17.0.2 |
Browser | Chrome | 92.0.4515.131 |
Assistive tech | N/A | |
Node | node | 14.17.5 |
npm/yarn | yarn | 1.22.10 |
Operating System | MacOS | 11.4 |
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Tabs displayed incorrectly when populated from array
Had a mistake in my code: <a href="#" id="${name}" class="tablinks" click.delegate="openTab($event, name)">${name}</a>. Should have been:
Read more >Tabs - Ant Design
Ant Design has 3 types of Tabs for different situations. Card Tabs: for managing too many closeable views. Normal Tabs: for functional aspects...
Read more >Tabs Widget | jQuery UI API Documentation
Disables a tab. The selected tab cannot be disabled. To disable more than one tab at once, set the disabled option: $( "#tabs"...
Read more >Tabs - Angular Material
API reference for Angular Material tabs. import {MatTabsModule} from '@angular/material/tabs'; ... Event emitted when the tab selection has changed.
Read more >JavaScript Ā· Bootstrap
DEFAULTS.keyboard = false // changes default for the modal plugin's ... Enable tabbable tabs via JavaScript (each tab needs to be activated individually):....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Can confirm this issue, I also ran into it. A quick and hacky solution would be adding a key to the
Tabs
component. That way youāll recreate the component every time you change the array of tabs. This may ir may not be an acceptable fix for you, depending on what youāre building.Forked and fixed the reproducible example: https://codesandbox.io/s/eager-nova-32edn?file=/src/App.tsx
Using a key worked well for me!