gpt4 book ai didi

javascript - 单击按钮后更改reactjs中的组件

转载 作者:行者123 更新时间:2023-12-02 13:54:36 25 4
gpt4 key购买 nike

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

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>
);
}
}

最佳答案

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

尝试这种方法:

Cloudsim配置

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>
);
}
}

关于javascript - 单击按钮后更改reactjs中的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40791996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com