React Tab component does not update active tab when Redux store value changes
P粉950128819
P粉950128819 2023-08-31 17:49:11
0
2
528
<p>I am using Chakra UI Tab component in my React application. I have a numeric value stored in Redux and I want to change the active tab based on that value. However, when I update the numerical value in the store, the active tab does not change accordingly. How can I solve this problem? </p> <p>Here is the code for the <code>MyTabs</code> component: </p> <pre class="brush:php;toolbar:false;">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> ); }</pre></p>
P粉950128819
P粉950128819

reply all(2)
P粉567281015

To ensure that the active tab in the Chakra UI Tab component is updated when the value in Redux changes, you can use the useEffect hook provided by React. The useEffect hook allows you to perform side effects such as updating the active tab when a specific dependency changes.

You can modify the MyTabs component as follows:

import { useEffect } from 'react';
    import { useSelector, useDispatch } from 'react-redux';
    import { Tabs, TabList, Tab, TabPanels, TabPanel } from '@chakra-ui/react';
    import { selectNumber, updateNumber } from 'path/to/your/redux/slice';
    function MyTabs() {
      const number = useSelector(selectNumber);
      const dispatch = useDispatch();
      useEffect(() => {
        // Update the active tab index in Redux when the number changes
        dispatch(updateNumber(number));
      }, [number, dispatch]);
      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>
      );
    }**'

In this updated code, the useEffect hook is used to dispatch an operation (updateNumber) when the value changes. Make sure you have implemented the appropriate operations and reducer logic (selectNumber) in your Redux slice to handle numeric updates.

By doing this, the active tab will automatically update based on the numerical value stored in Redux.

P粉953231781

defaultIndex The properties are:

See Controlled and Uncontrolled Components and Default Values ​​ Documentation:

You can use Controlled tab

const tabIndex = useSelector(selectnumber);
const dispatch = useDispatch();

<Tabs 
  index={tabIndex} 
  onChange={(index) => dispatch({ type: 'SET_TAB_INDEX', payload: { index } })}>
</Tabs>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!