我有4个组件。我只想一次渲染一个。我有我的导航按钮,当我点击一个它应该渲染该组件,然后隐藏其他3(即将它们设置为空)在点击按钮时切换组件的响应
这很容易与2个组件。我只是有一个切换功能,像这样:
toggle() {
this.setState(prevState => ({
showTable: !prevState.showTable
}));
}
我试图去适应这个现在在那里我有这样的:
showComponent(component) {
this.setState(prevState => ({
[component]: !prevState.component
}));
}
这目前显示当我点击相应的按钮组件。但是,一旦再次单击相同的按钮,它将不会隐藏组件。
我有我的所有按钮调用此方法像这样:
<button onClick={() => this.showComponent('AddPlayer')}>Add</button>
<button onClick={() => this.showComponent('ShowPlayers')}>Players</button>
<button onClick={() => this.showComponent()}>Table</button>
<button onClick={() => this.showComponent()}>Matches</button>
什么想法?
编辑:
{this.state.AddPlayer ?
<div className="add-container">
<AddPlayer />
</div>
:
null
}
{this.state.ShowPlayers ?
<div className="players-container">
<Players />
</div>
:
null
}
可以展开的问题,以显示正在显示/隐藏的组件? – Chris
你可以发布你的渲染方法吗?这样我可以回答好@The海象 –
是的我的坏,它的存在,现在 –