简体   繁体   English

如何在状态更改时停止ReactJS组件重新呈现

[英]How to stop reactjs component rerender on state change

I have a form with some input fields (some of them hidden until a checkbox is checked. When I check/uncheck the checkbox a specific state is set ( this.state.isChecked = true/false ). 我有一个带有一些输入字段的表单(其中一些隐藏,直到选中一个复选框。当我选中/取消选中该复选框时,会设置一个特定的状态( this.state.isChecked = true/false )。

On render method, i have a div (containing some input fields) with some classes and this condition for "show" class: <div className={'extra-fields ' + (this.state.isChecked? ' show' : '')}>...</div> 在渲染方法上,我有一个包含一些类的div(包含一些输入字段),并且该条件用于“显示”类: <div className={'extra-fields ' + (this.state.isChecked? ' show' : '')}>...</div>

Expected behavior is that on state change, only the "show" class is putted or deleted from div Current behavior: the entire form is rendered again and all data written in the input fields are lost. 预期的行为是在状态更改时,仅将“ show”类放入div或从div中删除当前行为:再次呈现整个表单,并且所有输入字段中写入的数据都会丢失。

How it should be done this? 应该怎么做呢?

If the value of the input fields is important (which they apparently are), and if they can change (which the obviously can), then React should be aware of them, typically in state. 如果输入字段的值很重要(显然很重要),并且可以更改(很明显可以更改),那么React应该知道它们,通常处于状态。
The 'standard' (on only) react way to maintain the contents of the input fields is: 维护输入字段内容的“标准”(仅启用)反应方式是:

  • put the content of the input fields in state as well, 也将输入字段的内容置于状态,
  • include something like value={this.state.foo} and onChange={this._onChange()} to the render of each input field 在每个输入字段的呈现中包含诸如value={this.state.foo}onChange={this._onChange()}之类的内容
  • include the _onChange() function to the form to handle input changes _onChange()包含_onChange()函数以处理输入更改

That way, whenever the form is re-rendered (after each setState() ), the input values are also preserved. 这样,每当重新呈现表单时(在每个setState() ),输入值也将被保留。

PS: The question title "stop reactjs component from rerender on state change" does not really cover the question from text: you are asking for a partial re-render: do re-render the show/hide extra fields based on checkbox, but do not re-render input fields. PS:问题标题“在状态更改时停止重新渲染ReactJS组件”并未真正涵盖文本中的问题:您要求部分重新渲染:请根据复选框重新渲染显示/隐藏额外的字段,但是不重新渲染输入字段。

The short answer is that you can prevent a component from rendering/updating with the shouldUpdateComponent life-cycle event: https://facebook.github.io/react/docs/react-component.html#shouldcomponentupdate 简短的答案是,您可以使用shouldUpdateComponent生命周期事件阻止组件渲染/更新: https ://facebook.github.io/react/docs/react-component.html#shouldcomponentupdate

However, I agree with the previous answer, that for the sake of doing what sounds like a partial re-render, and to follow best practices in general, you should be storing the state of your input fields somewhere in a component so that they persist beyond a render call. 但是,我同意前面的回答,即为了进行听起来像是部分重新渲染的操作,并且为了遵循一般的最佳做法,您应该将输入字段的状态存储在组件中的某个位置,以便它们持久存在除了渲染调用。

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

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