Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use React.findDOMNode function

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;

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.

like image 992
LazyDal Avatar asked Apr 04 '15 07:04

LazyDal


People also ask

Is findDOMNode deprecated?

Note: findDOMNode is an escape hatch used to access the underlying DOM node. In most cases, use of this escape hatch is discouraged because it pierces the component abstraction. It has been deprecated in StrictMode .

How do I fix warning findDOMNode is deprecated in StrictMode?

The Table component from the react-virtualized package is throwing a warning: Warning: findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of Grid which is inside StrictMode. Instead, add a ref directly to the element you want to reference.

How do you access the DOM element in react?

In React we can access the DOM element using Refs. Refs provide a way to access DOM nodes or React elements created in the render method. Creating Refs: Refs are created using React. createRef() and attached to React elements via the ref attribute.

Which component do we have to pass to the ReactDOM render () method?

render() The first argument is the element or component we want to render, and the second argument is the HTML element (the target node) to which we want to append it. Generally, when we create our project with create-react-app , it gives us a div with the id of a root inside index.


3 Answers

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

is sufficient.

like image 149
Roman Starkov Avatar answered Oct 13 '22 14:10

Roman Starkov


React.findDOMNode(component) was introduced in React 0.13.0 as a replacement for component.getDOMNode().

Make sure that you have React 0.13.1 installed. If you're using npm, you can run npm view react version to check which version of React is currently installed.

like image 14
Alexandre Kirszenberg Avatar answered Oct 13 '22 14:10

Alexandre Kirszenberg


The findDOMNode method has moved from react module to the react-dom module.

So you have import or require the ReactDOM from react-dom module then replace

var brand = React.findDOMNode(this.refs.brand).value.trim();

With

var ReactDOM = require('react-dom');
var brand = ReactDOM.findDOMNode(this.refs.brand).value.trim();
like image 5
Nahid Avatar answered Oct 13 '22 14:10

Nahid