Uncaught TypeError: _react2.default.findDOMNode is not a function

时间:2022-09-15 09:58:05

react 查找某节点时报错

Uncaught TypeError: _react2.default.findDOMNode is not a function

代码:

import React, {Component, PropTypes} from 'react';
class AddToDo extends Component {
render() {
return <div>
<input type="text" ref="input"/>
<button onClick={e => {
this.handleClick(e)
}}>Add</button>
</div>;
}
handleClick(e) { const node = React.findDOMNode(this.refs.input);
const text = node.value.trim();
this.props.onAddClick(text);
node.value = '';
}
}

原因是当前我使用的React 版本中没有findDOMNode函数。

改为引入react-dom

import ReactDOM, {findDOMNode} from 'react-dom';

 const node =  React.findDOMNode(this.refs.input);
改为
 const node = findDOMNode(this.refs.input);