【问题标题】:How to display different components when clicking a button on one page?单击一个页面上的按钮时如何显示不同的组件?
【发布时间】: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


【解决方案1】:

import React from 'react';
import logo from './logo.svg';
import './App.css';




function Statepicker(){
  return(
    <div>Statepicker</div>
  )
}

function Statecards(){
  return(
    <div>Statecards</div>
  )
}

function Countrypicker(){
  return(
    <div>Countrypicker</div>
  )
}

function Countrycards(){
  return(
    <div>Countrycards</div>
  )
}

class ThemeProvider extends React.Component{
  
  constructor(props){
     super(props);
     this.state={country:true,states:false}
     
  }

  renderCountryOrState=()=>{
    if(this.state.states){
      return(<React.Fragment>
        <Statepicker/>
        <Statecards/>
      </React.Fragment>)
    }
    return (
      <React.Fragment>
        <Countrypicker/>
        <Countrycards/>
      </React.Fragment>
    )
  }

  render(){
    return(
       <div>
         <button onClick={(e)=>{this.setState({country:true,states:false})}}>Select country</button>
         <button onClick={(e)=>{this.setState({country:false,states:true})}}>Select state</button>
         {this.renderCountryOrState()}
       </div>
    )
  }

}


function App() {
  return (
    <ThemeProvider/>
  );
}

export default App;

【讨论】:

    【解决方案2】:
    import React from "react";
    
    import { Button, View } from "react-native";
    import styles from "./App.module.css";
    import {View} from 'react-native'
    
    import image from "./images/covid1.png";
    
    class App extends React.Component {
      constructor(props){
          super(props);
          state = {
    
            CityButton: false,
            StateButton: false,
            CountryButton: false
          };
      }
    
      render() {
    
        { CountryButton, StateButton, CityButton } = this.state;
    
        return (
       <view>
          <div className={styles.container}>
            <img className={styles.image} src={image} alt="COVID-19" />
    
            <Button title="Country Mode" onPress={() => 
               this.setState({CountryButton: true}
               )} />
    
            <Button title="State Mode" onPress={() => 
               this.setState({StateButton: true}
               )} />
    
            <Button title="County Mode" onPress={() => 
               this.setState({CityButton: true}
               )} />
    
            {CountryButton && <div> Hello </div>}
    
            {StateButton && <div> Hello </div>}
    
            {CityButton && <div> Hello </div>}
          </div>
    </view>
        );
      }
    }
    
    export default App;
    

    【讨论】:

    • 我收到按钮未定义的错误
    • 从 'react-native' 导入 { Button };
    • 第 114:22 行:“CountryButton”未定义 no-undef 第 120:22 行:“StateButton”未定义 no-undef 第 126:22 行:“CityButton”未定义 no-undef我已经从 react-native 导入了 Button
    • 是的,CountryButton、StateButton、CityButton 是状态变量,当你按下按钮时将它们初始化为 false 并设置为 true。
    • 它不适合我,我会做一个沙箱给你看
    猜你喜欢
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多