I am using the Chakra UI Tab component in my React application. I have a number value stored in Redux, and I want to change the active tab based on this value. However, when I update the number value in the store, the active tab does not change accordingly. How can I fix this issue?
Here is the code for MyTabs
component:
function MyTabs() {
const number = useSelector(selectnumber);
console.log(number);
return (
<Tabs defaultIndex={number}>
<TabPanels>
<TabPanel>
<Image boxSize="200px" fit="cover" src="" />
</TabPanel>
<TabPanel>
<Image boxSize="200px" fit="cover" src="" />
</TabPanel>
</TabPanels>
<TabList>
<Tab>Naruto</Tab>
<Tab>Sasuke</Tab>
</TabList>
</Tabs>
);
}
2
Answers
The
defaultIndex
prop is:See Controlled and uncontrolled components and Default Values doc:
You can use Controlled Tabs