简体   繁体   English

this.refs是一个空对象,虽然它应该被设置

[英]this.refs is an empty object, although it should be set

I have a react component which is rendering a t-comb-form. 我有一个反应组件,呈现一个梳状形式。 The form seems to render alright. 表格似乎正常。

return (
  <form className="modal-body-form"
              autoCorrect="off"
              autoCapitalize="none"
              autoComplete="off"
              formMethod="post"
              onSubmit={function(e) {e.preventDefault();}}
            >
     <Form ref="form"
                onChange={this.onFormChange}
                type={eventSchema.struct}
                value={this.state.rawItem}
                options={eventSchema.options}/>
  </form>
);

But in the method "onFormChange" I want to call this.refs.form.getValue() which is usually working. 但是在方法“onFormChange”中我想调用this.refs.form.getValue()这通常是有效的。 But this time this.refs is an empty object. 但这次this.refs是一个空对象。 Any idea why it is empty? 知道为什么它是空的吗?

onFormChange: function (rawValue) {
    var modified = false;

    modified = isDataEqual(this.state.itemStoreState.selectedItem || {}, rawValue) === false;

    this.setState({
      rawItem: rawValue,
      modified: modified
    });

    this.refs.form.getValue(); // will automatically trigger the form validation 
}

as an info. 作为一个信息。 I am using react 0.13.3 with the old createClass syntax. 我正在使用旧的createClass语法反应0.13.3。 So the this should be correctly bound 所以这应该被正确绑定

From the react docs about refs ( http://facebook.github.io/react/docs/more-about-refs.html ). 来自refs的反应文档( http://facebook.github.io/react/docs/more-about-refs.html )。

For composite components, the reference will refer to an instance of the component class so you can invoke any methods that are defined on that class. 对于复合组件,引用将引用组件类的实例,以便您可以调用在该类上定义的任何方法。 If you need access to the underlying DOM node for that component, you can use ReactDOM.findDOMNode as an "escape hatch" but we don't recommend it since it breaks encapsulation and in almost every case there's a clearer way to structure your code within the React model. 如果你需要访问该组件的底层DOM节点,你可以使用ReactDOM.findDOMNode作为“逃生舱”但我们不推荐它,因为它打破了封装,几乎在所有情况下都有一种更清晰的方法来构建你的代码React模型。

Just noticed that you are using react 0.13. 刚注意到你正在使用react 0.13。 In that case try React.findDOMNode(this.refs.form). 在这种情况下,请尝试React.findDOMNode(this.refs.form)。

The error was in the library t-comb-form which I am using. 错误是我正在使用的库t-comb-form I had a form inside a form and the library was handling the inner form great, but somehow destroyed the refs from outside. 我在表单中有一个表单,并且库正在处理内部表单很好,但不知何故从外部销毁了refs。

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

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