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.

Controlled mode for Tabs component in React does not work

See original GitHub issue

What package within Headless UI are you using?

@headlessui/react

What version of that package are you using?

v1.6.3

What browser are you using?

Chrome

Reproduction URL

https://codesandbox.io/s/cool-https-tsn49q

Describe your issue

Controlled mode for Tabs component in React does not work, no matter if the state is inside of the component or outside, even if I would put selectedIndex={0} I still can change the tab.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

6reactions
RobinMalfaitcommented, Jul 15, 2022

Hey! Thank you for your bug report! Much appreciated! 🙏

@ptmdmusique your reproduction helped. This should be fixed by #1680, and will be available in the next release.

I also updated the CodeSandbox with the latest insiders version to see the fix in action: https://codesandbox.io/s/headless-ui-tabs-forked-h8h0lp?file=/src/App.js

You can already try it using:

  • npm install @headlessui/react@insiders.
  • npm install @headlessui/vue@insiders.
3reactions
ptmdmusiquecommented, Jul 1, 2022

Hey @RobinMalfait 👋 Thanks a bunch for such a helpful library 🎉

I think the problem OP posted was that we can still change the tab via Tab click even if it’s a controlled component and the internal state that’s used to manage the tabs index will be different from the controlled tab

Here is the repro sandbox: https://codesandbox.io/s/headless-ui-tabs-hf0lqr?file=/src/App.js

One use case I think this would help solve is when we need to validate something before moving to another tab (e.g: show a notice if there are unsaved changes on the current tab)

But maybe I miss something in the doc. Could you help me take a look ❓

Read more comments on GitHub >

github_iconTop Results From Across the Web

Switching between controlled mode (by using `selectedIndex ...
I ran into this same error while using this library. I was able to resolve it by making sure that the value passed...
Read more >
react-tabs - npm
Controlled mode​​ This mode has to be enabled by supplying selectedIndex to the <Tabs /> component. In this mode react-tabs does not handle...
Read more >
Uncontrolled Components - React
In a controlled component, form data is handled by a React component. The alternative is uncontrolled components, where form data is handled by...
Read more >
Tabs - Reach UI
Like form inputs, a tab's state can be controlled by the owner. Make sure to include an onChange as well, or else the...
Read more >
How to Build a Tabs Component with React - DigitalOcean
Tabs are a common UI component and are important to understand how ... If the project did not open in a browser window,...
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