- Home
- Forum
- React - EJ 2
- Transition each tab broken after setState
Transition each tab broken after setState
i have this code, the goals is to know wich one is tab are active. The problem with this code is the tranisiton when changing tab not working. maybe any other way to know which tab are active?
Hi muhammad
haikal,
Thanks for reaching out to us.
The issue you're facing might be due to the way you're managing the active tab state. You're using a useRef to hold a reference to the TabComponent, and then calling select on it to change the active tab. However, this might not trigger a re-render of the component, which is why the transition might not be working.
Instead, you
could manage the active tab state using useState and pass the active tab index
directly to the TabComponent as a prop. The TabComponent should automatically
handle the transition when the active tab changes.
[index.js]
|
const PrepopulatedFakturKeluaranContent = () => { const location = useLocation(); const navigate = useNavigate(); const [activeIndex, setActiveIndex] = useState(0);
const selected = (args) => { setActiveIndex(args.selectedIndex); };
useEffect(() => { const tab = location.state?.tab; if (tab !== undefined) { setActiveIndex(tab); navigate(location.pathname, { replace: true, state: {} }); } }, [location.state, navigate]);
return ( <Box pt="40px"> <Stack flexDirection="row" justifyContent="space-between" alignItems="center"> <Typography variant="h5" color="textBlack.main"> Prepopulated Faktur Keluaran </Typography> </Stack>
<TabComponent className="tab_content" selected={selected} selectedIndex={activeIndex}> <TabItemsDirective> <TabItemDirective headerTemplate={() => ( <HeaderTabTemplate title="Dalam Proses" point="10" isActive={activeIndex === 0} /> )} content={OnProgress} /> <TabItemDirective headerTemplate={() => ( <HeaderTabTemplate title="Selesai" point="20" isActive={activeIndex === 1} /> )} content={OnProgress} /> </TabItemsDirective> </TabComponent> </Box> ); }; |
Sample link: https://stackblitz.com/edit/react-sfzmbq-tehtwd?file=index.js,package.json
Don't hesitate to get in touch if you require further help or more information.
Regards,
Vijay
- 1 Reply
- 2 Participants
-
MH muhammad haikal
- Aug 16, 2024 03:28 PM UTC
- Aug 20, 2024 11:43 AM UTC