简体   繁体   English

多个this.setState(this.state)不重新呈现页面

[英]multiple this.setState(this.state) not re-rendering page

In my render, I have a function that updates the properties. 在渲染中,我有一个更新属性的函数。 I have listed the functions that get called between, but I think only the last one matters since it is the one that updates the data I use. 我已经列出了之间调用的函数,但是我认为只有最后一个很重要,因为它是更新我使用的数据的函数。

  <button 
    onClick={() =>
      this.handleUpdateProperty() 
    }>
    Update Properties 
  </button>

which calls: 哪个调用:

  handleUpdateProperty = () => {
    this.getDataBC(); 
    this.setState(this.state);
//db.inventory.find( { status: "D" } )
  }

That in turns calls: 依次调用:

  getDataBC = () => {
    var rentals = scFunctions.getRents();
    console.log(web3.toAscii(rentals[1][0]));


    for(let i = 0; i < rentals[0].length; i++){
      let currentProp = {
        status: rentals[0][i].toNumber(),
        location: web3.toUtf8(rentals[1][i]).replace(/\s+/g,''),
        company: web3.toUtf8(rentals[2][i]).replace(/\s+/g,''),
        price: rentals[3][i].toNumber(),
        start: rentals[4][i].toNumber(),
        end: rentals[5][i].toNumber(),
        help: "haha"
      }
      console.log(currentProp)
      this.updateDB(currentProp);
    }
    this.getDataFromDb(); 
    this.setState(this.state);
  };

That in turn calls: 依次调用:

  getDataFromDb = () => {
    fetch("http://localhost:3001/api/property")
      .then(property => property.json())
      .then(res => this.setState({ data: res.data }))
      .then(this.setState(this.state))
  };

The last function does the: 最后一个函数执行以下操作:

`.then(res => this.setState({ data: res.data }))`

which updates the data I use to render my page. 会更新用于呈现页面的数据。 However, it doesn't update the page right away, I have to refresh the page to see the results from pressing the button. 但是,它不会立即更新页面,我必须刷新页面才能查看按按钮的结果。 I thought .then(res => this.setState({ data: res.data })) would rerender the page? 我以为.then(res => this.setState({ data: res.data }))会重新渲染页面吗?

Thank you so much 非常感谢

edit: The constructor is as follows: 编辑:构造函数如下:

  constructor(props) {
    super(props); 
    this.state = {
      data: [],
      show: false, // show of the rental modal
      company: "Homeaway", 
      id: 0,
      message: null,
      intervalIsSet: false,
      idToDelete: null,
      idToUpdate: null,
      objectToUpdate: null,
      rentProperty: "DongFang",
      startDate: new Date(),
      endDate: new Date(),
      firstName: "Ludwig",
      showConflict: true,
      lastName: "Wittgenstein"
    };
    this.handleCompanySubmit = this.handleCompanySubmit.bind(this);

  }

This is what uses the "data" from state. 这就是使用状态中的“数据”的原因。 So I want this function to rerun and update the page when I setState...: 所以我想在setState ...时重新运行此功能并更新页面:

  renderProperties = data => {
    var properties = []
    var propRow = []
    data.forEach((property,index) => {
      propRow.push(<Col xs={{ size:3, offset: .5}}> 
        <Jumbotron>
          <Image src={require("./images/1.jpg")} fluid rounded />
          <b> {property.location} </b> 
          <h1> Price: {property.price} </h1> 
          <div> 
          {this.renderStatusButton(property)}
          </div>
        </Jumbotron> 
      </Col>)
      if((index+1)%3 == 0){ // if first in the row
        properties.push(<Row>{ propRow }</Row>)
        propRow = []
      }

    })
    return (
      <Container>
        {properties}
      </Container> 
    )
  }

And this is in the render: 这是在渲染中:

   {this.renderProperties(data)} 

I am going to bed. 我要睡觉了。 Thank you all for your help so far. 谢谢大家到目前为止的帮助。 If it doesn't get fixed, it is fine. 如果不固定,那就很好。 It is not pivotal. 这不是关键。

If I'm correct, you just want to refresh the page once the fetch in getDataFromDb() has finished, is that correct? 如果我是对的,那么您只想在getDataFromDb()的提取完成后刷新页面,对吗?
If so, you don't need all those setState() calls, you just need one in getDataFromDb() , which should be written as follow: 如果是这样,则不需要所有这些setState()调用,而只需要在getDataFromDb()一次调用,就可以这样编写:

getDataFromDb = () => {
    fetch("http://localhost:3001/api/property")
      .then(property => property.json())
      .then(res => this.setState({ data: res.data }))
};

That is, you don't need the last setState() call you wrote neither. 也就是说,您不需要最后编写的两个setState()调用。

Anyways, in getDataBC() I see two functions ( getRent() and updateDB ) that I don't know what they do, so maybe there are some problems in those functions too. 无论如何,在getDataBC()我看到了两个我不知道它们做什么的函数( getRent()updateDB ),因此这些函数中可能也存在一些问题。

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

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