无法使用React.findDOMNode函数 [英] Can't use React.findDOMNode function

查看:26
本文介绍了无法使用React.findDOMNode函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我无法使用React.findDOMNode函数.浏览器抱怨类型错误,说React.findDOMNode不是一个函数.这是发生这种情况的代码:

For some reason, I'm not able to use React.findDOMNode function. Browser complains about type error, saying React.findDOMNode is not a function. This is the code where this happens:

var React = require('react');
var Backbone = require('backbone');
var Car = require('models/car');
var NewCarForm = React.createClass({
  handleSubmit: function(e) {
    e.preventDefault();
    var brand = React.findDOMNode(this.refs.brand).value.trim();
   ...
    this.props.handleNewCar(new Car({brand: brand, model:model,   name:name, kmTraveled:odometer, litresSpent:litres}));
    return;
  },
  render: function() {
    console.log("Inside NewCarForm");
    return (
      <form className="contentSection" onSubmit={this.handleSubmit}>
        <input type="text" placeholder="Car Brand" ref="brand" />
       ...
        <input type="submit" value="Post" />
      </form>
    );
  }
});
module.exports = NewCarForm;

这是我尝试使用此功能的唯一模块.React的其余部分工作正常,所以我不知道这里可能是什么问题.

This is the only module where I try to use this function. The rest of React works fine, so I have no idea what could be the problem here.

推荐答案

在React 15中(可能不确定某些早期版本,我不确定),您不需要 findDOMNode 在以下位置使用引用全部:

In React 15 (and possibly some earlier versions, I'm not sure) you don't need findDOMNode to use refs at all:

this.refs.brand.value

足够了.

这篇关于无法使用React.findDOMNode函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆