У меня есть этот статический компонент содержимого, который не перерисовывает
static Content = ({ children, tabId, activeTab }) => {
return tabId === activeTab ? children : ''
}
tabId и activeTab отображаются с помощью React.cloneElement.
render() {
return React.Children.map(this.props.children, child =>
React.cloneElement(child, {
handleTabClick: this.handleTabClick,
tabId: this.props.id,
activeTab: this.state.activeTab
})
);
}
Но я понятия не имею, почему содержимое вкладки 1 не скрывается, когда я нажимаю на заголовок вкладки 2.
Вот демо https://codesandbox.io/s/w2mxm3zjq5





Поскольку ваш компонент Tab хранит это состояние, при щелчке по нему обновляется только конкретное состояние компонента и, следовательно, другое не изменяется, вам нужно поднять состояние до компонента вкладок, и он будет работать.
class Tab extends Component {
static Title = ({ children, handleTabClick, tabId }) => {
return <div onClick = {() => handleTabClick(tabId)}>{children}</div>;
};
static Content = ({ children, tabId, activeTab }) => {
console.info(tabId, "a", activeTab);
return tabId === activeTab ? children : "";
};
render() {
return React.Children.map(this.props.children, child =>
React.cloneElement(child, {
handleTabClick: this.props.handleTabClick,
tabId: this.props.id,
activeTab: this.props.activeTab
})
);
}
}
class Tabs extends React.Component {
state = {
activeTab: this.props.activeTab
};
handleTabClick = id => {
this.setState({
activeTab: id
});
};
render() {
const { children, activeTab } = this.props;
return React.Children.map(children, child =>
React.cloneElement(child, {
activeTab: this.state.activeTab,
handleTabClick: this.handleTabClick
})
);
}
}
const App = () => {
return (
<Tabs activeTab = {1}>
<Tab id = {1}>
<Tab.Title>Tab 1</Tab.Title>
<Tab.Content>Tab 1 content goes here</Tab.Content>
</Tab>
<Tab id = {2}>
<Tab.Title>Tab 2</Tab.Title>
<Tab.Content>Tab 2 content goes here</Tab.Content>
</Tab>
</Tabs>
);
};
На самом деле я рекомендую вам изменить логику, которой вы следуете ... Состояние, содержащее activeTab, должно быть частью
Tabs, а не самимTab