I am using React Js in my project and the Material UI Tabs component to navigate between 2 components.
When I go to Tab 2 or Tab 1 by clicking on it, my component renders and the route gets changed but the tab indicator does not get updated on the active tab.
I've already tried to update the state on the button click but it isn't working
class NavBars extends React.Component {
state = {
value: 0,
};
handleChange = (event, value) => {
this.setState({ value });
console.log(this);
};
navigateToTab2 = () => {
this.setState({ value: 1 });
console.log(this.state);
this.props.history.push('/tab2');
}
navigateToTab1 = (facility, event) => {
this.setState({ value: 0 });
console.log(this.state);
this.props.history.push('/tab1', {
facilityId: facility.facilityId,
facilityName: facility.facilityName,
});
}
render() {
const { classes } = this.props;
const { value } = this.state;
return (
<div className={classes.root}>
<Tabs
value={value}
onChange={this.handleChange}
classes={{ root: classes.tabsRoot, indicator: classes.tabsIndicator }}
>
<Tab
disableRipple
classes={{ root: classes.tabRoot, selected: classes.tabSelected }}
label="Tab 1"
onClick={this.navigateToTab1}
/>
<Tab
disableRipple
classes={{ root: classes.tabRoot, selected: classes.tabSelected }}
label="Tab 2"
onClick={this.navigateToTab2}
/>
</Tabs>
</div>
);
}
}
I expect that when I click on Tab1 it should go to Tab1 and render that component with Tab indicator be on Tab 1 and when I click on Tab 2 it should go to Tab 2 and render the Tab 2 component with tab indicator on tab 2
