【问题标题】:Changing a component in reactjs upon clicking a button单击按钮时更改 reactjs 中的组件
【发布时间】:2016-11-24 17:28:41
【问题描述】:

我有一个带有 for 按钮的菜单,每个按钮指向不同的表单。所以基本上我只希望在单击每个按钮时出现一个相应的表单。我当前的代码没有更新组件,但正在调用函数:

export default class Configuration extends Component{

    constructor(props){
        super(props);
        this.state ={
            response:"",
        };
        currentMode ='upload';
        this.getForms = this.getForms.bind(this);

    }

    componentWillMount(){
        this.getForms('upload');
    }

    getForms(current_mode){
        console.log('im in', current_mode);
        switch(current_mode){
            case 'form1':
                return (<div><Form1/></div>);
            case 'form2':
                return (<div><Form2/></div>);
            case 'form3':
                return (<div><Form3/></div>);
            case 'form4':
                return (<div><Form4/></div>);
        }
    }

    render(){
        return (
            <div>
                        <ConfigurationMenu getForms={this.getForms}/>
                        {this.getForms(currentMode)}
            </div>

    }
}

// here are the buttons:
class ConfigurationMenu extends Component{

    constructor(props){
        super(props);
        this.state={key:1}
    }

    handleSelect(key, formCategory){
        console.log('hiiii', formCategory);
        this.props.getForms(formCategory);
        currentMode=formCategory;
        this.setState({key:key});
    }

    render(){
        return (
            <Nav bsStyle="tabs" activeKey={this.state.key}>
              <NavItem eventKey={1} title="Form1" onClick={()=>this.handleSelect(1, 'form1')}>Form1</NavItem>
              <NavItem eventKey={2} title="Form2" onClick={()=>this.handleSelect(2, 'form2')}>Form2</NavItem>
              <NavItem eventKey={3} title="Form3" onClick={()=>this.handleSelect(3, 'form3')}>Form3</NavItem>
              <NavItem eventKey={4} title="Form4" onClick={()=>this.handleSelect(4, 'form4')}>Form4</NavItem>
            </Nav>
        );
    }
}

【问题讨论】:

    标签: javascript reactjs react-jsx jsx


    【解决方案1】:

    如果我的理解是正确的,您想更改单击ConfigurationMenu 中的按钮时呈现的表单组件。

    试试这个方法:

    CloudsimConfiguration

    export default class CloudsimConfiguration extends Component {
    
      constructor(props) {
        super(props);
        this.state = {
          response: '', // I am not sure about the purpose of this, leaving it as it is
          currentMode: 'form1',
        };
      }
    
      // returns the corresponding Form based on currentMode
      getForm(currentMode) {
        const forms =  {
          form1: <Form1/>,
          form2: <Form2/>,
          form3: <Form3/>,
          form4: <Form4/>
        };
    
        return forms[currentMode];
      }
    
      // update currentMode when ConfigurationMenu triggers the callback
      toggleForm(currentMode) {
        this.setState({ currentMode });
      }
    
      render() {
        return (
          <div>
            <ConfigurationMenu toggleForm={this.toggleForm} />
            <div>
              {this.getForm(this.state.currentMode)}
            </div>
          </div>
        );
      }
    }
    

    配置菜单

    class ConfigurationMenu extends Component {
      constructor(props) {
        super(props);
        this.state = { key: 1 };
      }
    
      handleSelect(key, formCategory) {
        this.props.toggleForm(formCategory);
        this.setState({ key });
      }
    
      render(){
        return (
          <Nav bsStyle="tabs" activeKey={this.state.key}>
            <NavItem eventKey={1} title="Form1" onClick={() => this.handleSelect(1, 'form1')}>Form1</NavItem>
            <NavItem eventKey={2} title="Form2" onClick={() => this.handleSelect(2, 'form2')}>Form2</NavItem>
            <NavItem eventKey={3} title="Form3" onClick={() => this.handleSelect(3, 'form3')}>Form3</NavItem>
            <NavItem eventKey={4} title="Form4" onClick={() => this.handleSelect(4, 'form4')}>Form4</NavItem>
          </Nav>
        );
      }
    }
    

    【讨论】:

    • 完美运行。谢谢。
    猜你喜欢
    • 2020-07-20
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 2016-07-11
    • 2021-10-06
    • 2020-08-21
    • 1970-01-01
    • 2017-10-12
    相关资源
    最近更新 更多