【发布时间】:2020-05-09 10:24:06
【问题描述】:
当我在 App.js 的渲染功能中单击带有 OnClick 的按钮时,我试图显示不同的组件。
我想在单击按钮时显示某个组件并隐藏其他组件。
这是我想做的一个例子
return (
<div className={styles.container}>
<img className={styles.image} src={image} alt="COVID-19" />
//If the Country Button which is the default is clicked show This
<ThemeProvider theme = {theme}>
<CountryPicker handleCountryChange={this.handleCountryChange} />
<CountryCards CountryData = {CountryData} CountryYesterdayData = {CountryYesterdayData}/>
</ThemeProvider>
<Chart countrydailydata ={countrydailydata} />
//If the State Button is clicked show this
<ThemeProvider theme= {theme}>
<StatePicker handleStateChange={this.handleStateChange} />
<StateCards stateData= {stateData} yesterdayStateData = {yesterdayStateData}/>
</ThemeProvider>
//If the City Button is clicked show this
<CityPicker handleCityChange={this.handleCityChange}/>
<CityCard cityData = {cityData}/>
</div>
);
【问题讨论】:
-
您可以通过将状态合并到您的应用程序中来做到这一点,使应用程序的默认“位置”状态为“国家”和您的一个按钮的 onClick 事件,将状态设置为另一个位置,然后根据状态中位置的值呈现您的应用程序
标签: javascript reactjs