【问题标题】:ReactJS is it normal that class rendering twice while I set the stateReactJS 在我设置状态时类渲染两次是否正常
【发布时间】:2020-06-25 11:54:55
【问题描述】:

我正在做自动完成课程,所以我想在输入值的每次更改中设置状态。 喜欢:

state = {
  value: ""
}

render(){
  console.log("called");
  return(
    <div>
      <input type="text" onChange={(e) => this.setState({value: e.target.value})/>
    </div>
  );
}

所以在控制台中,每次更改我都会被“调用”两次。 请问一下好不好用?

【问题讨论】:

标签: reactjs


【解决方案1】:

您不应该以这种方式将值分配给状态,基于类的组件的推荐方式是使用 this.setState()

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-11
  • 1970-01-01
相关资源
最近更新 更多