简体   繁体   English

我需要在按钮上单击两次以显示组件

[英]I need to click twice on ,the button to display the component

In this react component, I want to render the data to the screen when the 'Submit' button is clicked by the user, but the button has to be pressed twice for the data to display.在这个反应组件中,我想在用户单击“提交”按钮时将数据呈现到屏幕上,但必须按两次按钮才能显示数据。

Below is my code snippet:下面是我的代码片段:

class Invoice extends Component {
  constructor(props) {
    super(props);
    this.state = { items: [], isLoaded: false, transId: "" ,flag:true,errorFlag:false,show:false,displayContent:"",invoice:"",invoiceXmlBody:"",invoiceResult:"",invoiceTransId:"",invoiceResultFlag:false,invoicedisplayFlag:false};

  }
handleChangeInvoice(e) {
    console.log("inside handleChangeInvoice");
    let invoiceXml = e.target.value;
    if (invoiceXml !== undefined) {
      this.setState({ invoiceXmlBody: e.target.value });
    }
  }

  handleSubmitInvoiceXml =e=>{
   console.log("*******************inside handleSubmitInvoiceXml***************");
   let url = "http://localhost:8080/postInvoiceXml";
   fetch(url, {
    method: "POST",
    body: JSON.stringify(this.state.invoiceXmlBody),
     })
     .then(res => res.json()).then(data=>
       {
    this.setState({items:data});
    console.log(this.state.items[0].status);
     console.log("response========="+JSON.stringify(data));
      if(data===undefined){
          this.state.invoiceResultFlag=true;
    }
    else{
      this.state.invoicedisplayFlag=true;
          }
    }
 ) }

  render() {
    console.log("---------------------------"+this.state.invoicedisplayFlag);
    return (
      <div className="App">
        <br/>
          <label className="lable" style={{marginRight:19}}>
              InvoiceXml:
              <input
                type="text"
                name="invoiceXml" placeholder="Enter invoice xml"
                onBlur={this.handleChangeInvoice.bind(this)}  style={{marginLeft:15}} />

            </label><br/><br/>
             <input type="button" onClick={this.handleSubmitInvoiceXml} name="Submit" value="Submit" className="submitButton" />          
            <br/>
            <br/><br/>

            <div  className={this.state.invoicedisplayFlag?"showDisplay":"hide"}>
              {this.state.items.map((item,i) => (
                <div>
                <h2>Invoice No is: {item.invoiceNo}</h2>
                 <h2>Transaction id is: {item.transId}</h2>
                  <h2>Status: { item.status ? 'Success' : 'Failed' }</h2>
                 </div>
                       ))}
               </div>
       </div>
    );
  }
}

how can I resolve this?我该如何解决这个问题?

it is easy to find why, basically you are setting a value in your state directly, you should do it using setState() like the documentation says很容易找到原因,基本上你是直接在你的状态中设置一个值,你应该像文档说的那样使用setState()来做

so your code is:所以你的代码是:

if(data===undefined){
    this.state.invoiceResultFlag=true;
}
else {
    this.state.invoicedisplayFlag=true;
}

but it should be:但它应该是:

if(data===undefined){
    this.setState({invoiceResultFlag: true})
}
else {
    this.setState({invoicedisplayFlag: true});
}

another thing that I noticed is that you want to set an state after you set an state, then you can use the callback from setState()我注意到的另一件事是您想在设置状态后设置状态,然后您可以使用setState()的回调

this.setState({items:data}, ()=>{
    if(data===undefined){
        this.setState({invoiceResultFlag: true})
    }
    else {
        this.setState({invoicedisplayFlag: true});
    }
});

and finally, I dont personally like those 2 setState, it would be easier to do it everything in one place:最后,我个人不喜欢那些 2 个 setState,在一个地方完成所有事情会更容易:

this.setState({
    items:data,
    invoiceResultFlag: !data,
    invoicedisplayFlag: !!data,
});

在该按钮的 onClick 函数中,只需在组件状态中保存一个标志 [或本地到您的类] 指示按钮是否被单击,如果是,则执行渲染逻辑并再次重置该标志

I ran into a similar problem where I was using onBlur to update the state of an input value.我在使用 onBlur 更新输入值的状态时遇到了类似的问题。

I found changing onClick to onMouseDown to work我发现将 onClick 更改为 onMouseDown 可以工作

<input className="input-form" onBlur={(e)=>{this.setState({field1:e.target.value})}/>

<button className="submit-button" onMouseDown={()=>{this.submitForm()}}>Submit</button>

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

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