简体   繁体   English

如何'重置'ReactJS元素?

[英]How to 'reset' a ReactJS element?

I'm trying to 'reset' a ReactJS element. 我正在尝试“重置”一个ReactJS元素。

In this case, the element is 90%+ of the contents of the page. 在这种情况下,元素是页面内容的90%以上。

I'm using replaceState to replace the state of the element with with its initial state. 我正在使用replaceState将元素的状态替换为其初始状态。

Unfortunately, sub-elements which have their own 'state' do not reset. 不幸的是,具有自己的“状态”的子元素不会重置。 In particular, form fields keep their contents. 特别是,表单字段保留其内容。

Is there a way of forcing a re-render of an element, which will also cause sub-elements to re-render, as if the page had just loaded? 有没有办法强制重新渲染元素,这也会导致子元素重新渲染,就好像页面刚刚加载一样?

Adding a key to the element forces the element (and all its children) to be re-rendered when that key changes. 向元素添加key会强制在该键更改时重新呈现元素(及其所有子元素)。

(I set the value of 'key' to simply the timestamp of when the initial data was sent.) (我将'key'的值设置为简单地发送初始数据的时间戳。)

render: function() {
  return (
    <div key={this.state.timestamp} className="Commissioning">
      ...

The this.replaceState(this.getInitialState()) method doesn't actually reset children that are inputs, if that's what you're looking for. this.replaceState(this.getInitialState())方法实际上不会重置输入的子this.replaceState(this.getInitialState()) ,如果这是你正在寻找的。 For anyone looking to just reset their form fields, there is a standard DOM reset() function that will clear all the inputs in a given element. 对于任何想要重置其表单字段的人来说,有一个标准的DOM reset()函数可以清除给定元素中的所有输入。

So with React, it'd be something like this: 所以使用React,它会是这样的:

this.refs.someForm.getDOMNode().reset();

Doumentation: Doumentation:

https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/reset https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/reset

If it is a form you want to reset, you simply can use this 如果它是您要重置的表单,您只需使用它即可

// assuming you've given {ref: 'form'} to your form element
React.findDOMNode(this.refs.form).reset();

While I don't personally think you should store local, interim component state (like in-progress input boxes) in a centralized location (like a flux store) in most cases, here it may make sense, depending on how many you have, especially since it sounds like the inputs already have some server interaction/validation around them. 虽然我个人认为你不应该在大多数情况下将本地的临时组件状态(如正在进行的输入框)存储在一个集中的位置(如磁通存储),但这里可能有意义,具体取决于你拥有的数量,特别是因为它听起来像输入已经围绕它们进行了一些服务器交互/验证。 Pushing that state up the component hierarchy or into some other central location may help a lot in this case. 在这种情况下,将该状态推到组件层次结构或其他中心位置可能会有很大帮助。

One alternative idea off the top of my head is to use a mixin in components that might need to reset local state, and do some kind of event triggering, etc. to make it happen. 我最喜欢的另一个想法是在可能需要重置本地状态的组件中使用mixin,并执行某种事件触发等,以实现它。 For example, you could use Node's EventEmitter or a library like EventEmitter3 with a mixin like this (warning: not tested, maybe best this as pseudocode :) 例如,您可以使用Node的EventEmitter或像EventEmitter3这样的库与这样的mixin(警告:未测试,也许最好这个伪代码:)

var myEmitter = new EventEmitter(); // or whatever

var ResetStateMixin = {
  componentWillMount: function() {
    myEmitter.on("reset", this._resetState);
  },

  componentWillUnmount: function() {
    myEmitter.off("reset", this._resetState);
  },

  _resetState: function() {
    this.replaceState(this.getInitialState());
  },

  triggerReset: function() {
    myEmitter.emit("reset");
  }
};

Then you could use it in components like so: 然后你可以在像这样的组件中使用它:

React.createClass({
  mixins: [ResetStateMixin],

  getInitialState: function() {
    return { ... };
  },

  onResetEverything: function() {
    // Call this to reset every "resettable" component
    this.triggerReset();
  }
});

This is very basic and pretty heavy handed (you can only reset all components, every component calls replaceState(this.getInitialState()) , etc.) but those problems could be solved by extending the mixin a bit (eg having multiple event emitters, allowing component-specific resetState implementations, and so forth). 这是非常基本和非常繁重的(你只能重置所有组件,每个组件调用replaceState(this.getInitialState())等)但这些问题可以通过扩展mixin来解决(例如,有多个事件发射器,允许特定resetState组件的resetState实现,等等。

It's worth noting that you do have to use controlled inputs for this to work; 值得一提的是, 必须使用控制投入这个工作; while you won't need to push your state all the way up the component hierarchy, you'll still want all your inputs to have value and onChange (etc.) handlers. 虽然您不需要将状态推到组件层次结构中,但您仍然希望所有输入都具有valueonChange (等)处理程序。

You could also use 你也可以用

document.forms[0].reset()

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

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