javascript - 我需要点击两次按钮来显示组件

标签 javascript reactjs dom

在这个 React 组件中,我想在用户单击“提交”按钮时将数据呈现到屏幕上,但必须按下按钮两次才能显示数据。

下面是我的代码片段:

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

我该如何解决这个问题?

最佳答案

很容易找到原因,基本上您是直接在状态中设置一个值,您应该使用 setState() like the documentation says

所以你的代码是:

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

但它应该是:

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

我注意到的另一件事是你想在设置状态后设置状态,那么你可以使用 setState()

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

最后,我个人不喜欢那两个 setState,在一个地方完成所有事情会更容易:

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

关于javascript - 我需要点击两次按钮来显示组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54133821/

相关文章:

javascript - 迭代包含要使用结果的对象的对象

node.js - 如何修复错误:only absolute URLS are supported in react ssr graphql

php - 如何使用 dom 和 php 获取 td 值

java - 逆序解析json列表

javascript - 带 JS 的 Internet Explorer 11 性能

javascript - 如何识别被点击的图片?

python - 没有递归搜索python的xml解析

php - Code Igniter Form - PHP 变量中的 Javascript。不适合胆小的人

reactjs - 当我有 `display: none` 时,将调用 ComponentDidMount 函数

javascript - 如何获取 Chrome/WebKit 中过渡元素的当前颜色?