-
Notifications
You must be signed in to change notification settings - Fork 7
Error when closing only existing tab. #15
Description
Thanks for this simple component.
I was able to get it working on my project only after I added activeIndex to my state and default it to 0: activeIndex: 0;. I also had to bind the addTab function to this: this.addTab = this.addTab.bind(this);. These are not included in your code example and it won't work for me otherwise.
However, I run into errors when I close the only remaining tab. I get the same error if I don't initialize the tab state with at least one tab to display. These are the same issue, where <CloseableTabs/> is not able to accept an empty array of tabs within data. For my React page containing a tabs component, there may be 0 to many tabs.
In order to keep parent component from dying with the error, I am handling the component display as follows where it is conditionally displayed depending on tabs array length:
...
<div>
{this.state.tabs.length > 0 && (
<CloseableTabs
tabPanelColor="lightgray"
data={this.state.tabs}
onCloseTab={(id, newIndex) => {
this.setState({
tabs: this.state.tabs.filter(item => item.id !== id),
activeIndex: newIndex
});
}}
activeIndex={this.state.activeIndex}
/>
)}
</div>
...
This will hide the CloseableTabs component when there are no tabs to display and not bring down my app, but the error shows in the console log.
Do you have a current code example of how you handle this scenario?