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.
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 .
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.
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.
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.
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With