question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

[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:

  1. 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.
  2. 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.
  3. 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

CodeSandbox 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:open
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
okjuliancommented, Jan 15, 2022

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

0reactions
jasikparkcommented, May 26, 2022

Using a key worked well for me!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found