简体   繁体   English

this.forceUpdate()不会重新呈现动态创建的组件

[英]this.forceUpdate() not re-rendering dynamically created components

Assume all the various components have been defined. 假设所有各种组件均已定义。

In my react component, I want the button click to trigger the appending of a new TextBox component in my dynamically created questions component. 在我的react组件中,我希望单击按钮可以触发在我动态创建的questions组件中添加新的TextBox组件。 When I tested the button click with forceUpdate() , a TextBox was successfully appended to questions but there was no apparent addition of a new TextBox element. 当我使用forceUpdate()测试按钮单击时,成功将TextBox附加到questions但是显然没有添加新的TextBox元素。 I tested whether the component was actually re-rendering by using <h4>Random number : {Math.random()}</h4> and it turns out the component was doing so, as the number changed every time I pressed the button. 我通过使用<h4>Random number : {Math.random()}</h4>测试了组件是否确实在重新渲染,事实证明,组件正在这样做,因为每次按下按钮时数字都会改变。

Is something being done wrong? 做错什么了吗?

constructor (props) {
  super(props);
  this.questions = [];
  this.questions.push(<TextBox key={this.questions.length}/>);
  this.createTextBox = this.createTextBox.bind(this);
  this.loadTextBox = this.loadTextBox.bind(this);
}

createTextBox() {
  this.questions.push(<TextBox key={this.questions.length}/>);
  this.forceUpdate();
}

loadTextBox() {
  return (this.questions);
}

render() {
  return(
    <div>
      <h4>Random number : {Math.random()}</h4>
      {this.loadTextBox()}
      <ButtonToolbar className="add-question">
        <DropdownButton bsSize="large" title="Add" id="dropdown-size-large" dropup pullRight>
          <MenuItem eventKey="1" onClick={this.createTextBox}>Text Box</MenuItem>
        </DropdownButton>
      </ButtonToolbar>
    </div>
  );
}

Only items inside this.state are properly monitored by React on whether or not a rerender should occur. React仅在this.state中的项目上是否应进行重新渲染进行监视。 Using this.forceUpdate does not check to see if this.questions has been changed. 使用this.forceUpdate不会检查this.questions是否已更改。

Use this.questions as this.state.questions . 使用this.questions作为this.state.questions When you do this, do not mutate this.state.questions . 执行此操作时,请勿this.state.questions Instead, make a new copy of it and use this.setState on it. 相反,请为其制作一个新副本并在其上使用this.setState

constructor (props) {
  super(props);
  this.state = {
    questions: [<TextBox key={0}/>]
  }
  this.createTextBox = this.createTextBox.bind(this);
  this.loadTextBox = this.loadTextBox.bind(this);
}

createTextBox() {
  const newQuestions = [...this.state.questions, <TextBox key={this.questions.length}/>]
  // or you can use 
  // const newQuestions = this.state.questions.concat(<TextBox key={this.questions.length + 1}/>)
  this.setState({questions: newQuestions})
}

loadTextBox() {
  return (this.state.questions);
}

One important thing to note is that this.forceUpdate is almost never needed. 需要注意的一件事是,几乎不需要this.forceUpdate If you find yourself using it, you are writing your code in an unoptimal way. 如果发现自己正在使用它,则说明您的代码编写方式不是最佳的。 I made some modifications to your code regarding how keys are assigned. 我对您的代码进行了一些修改,有关密钥的分配方式。 The only reason you should ever be checking for updates is if something in this.state has changed, which involves using this.setState . 您应该检查更新的唯一原因是this.state中的某些this.state是否已更改,这涉及使用this.setState

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

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