简体   繁体   English

使用ReactJS显示/隐藏多个按钮

[英]Show/Hide multiple buttons with ReactJS

I'm trying to show/hide different buttons with ReactJS for the purpose of multiple view option and I'm stuck. 我试图使用ReactJS显示/隐藏不同的按钮,以实现多视图选项,但我被卡住了。

I'm running some queries based on user input with reactJS . 我正在基于user inputreactJS运行一些查询。 I want to make it possible for the user to have multiple views of the same query and for that my Idea was to use react-router and navigate between pages ( as views ) with the help of buttons. 我想让用户可以拥有同一查询的多个视图,并且我的想法是使用react-router并借助按钮在页面之间(作为视图)进行导航。 So far I understood that it's possible to save the user input into variable and use it with the router , but first I wanted to know how to create buttons after the user submits his input. 到目前为止,我知道可以将用户输入保存为变量并与router一起使用,但是首先,我想知道如何在用户提交输入后创建按钮。

My Idea was to create the button and hide it and then unhide it from the call Function. 我的想法是创建按钮并将其隐藏,然后从调用Function中取消隐藏它。 But I don't know how can I change the style of other button not the that that was clicked. 但是我不知道如何更改其他按钮的样式,而不是单击的样式。

If this isn't good practice how can I create new button from within the function that is called onSubmit ? 如果这不是一个好习惯,我如何从名为onSubmit的函数中创建新按钮?

My Code: 我的代码:

handleSubmit(event) {
event.preventDefault();
const nBtn = this.otherBtn.value;

//hidden to false -- how ?


//run query
}

 render() {
return(
<form onSubmit={this.handleSubmit}>
   <FormGroup>
   <FormControl type="text" placeholder="Search for xy" inputRef={(ref) => {this.xy= ref}}  />
   </FormGroup>
   <Button className="searchXY" type="submit" bsStyle="success">Search</Button>
   </form>
   <br />
   <Button className="btnRouter" id='testBtn' type="submit" bsStyle="danger" hiden={true} inputRef={(ref) => {this.otherBtn= ref}}  >See results as abcd </Button>
  );
}

I'm thinking you are wanting to show the See result as abcd Button after you have submitted your Form... If so - you may want to call this.setState in your handleSubmit and make use of Reacts conditional rendering To show/hide your button based on the value of this.state.showButton 我认为您要在提交表单后显示“将See result as abcd按钮显示...”。如果是,则可能需要在this.setState中调用this.setState并利用Reacts 条件渲染来显示/隐藏您的按钮基于this.state.showButton的值

 constructor (props) { super(props) this.state = { showbutton: false } this.handleSubmit = this.handleSubmit.bind(this) } handleSubmit () { this.setState({ showButton: true }) } render () { return ( <div> <form onSubmit={ this.handleSubmit }> <input type="text" /> <button type="submit" /> </form> {this.state.showButton ? <button>See as abcd</button> : null} </div> ) } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM