【发布时间】:2017-10-17 04:32:57
【问题描述】:
我试图在单击添加新按钮时模糊日历控件,但它会引发错误。如果有人知道解决方案,请回答,实际上 ref 是造成问题的原因:Uncaught Error: Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding a ref to a component that was not created inside a component's
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello React!</title>
<script src="../react.js"></script>
<script src="../react-dom.js"></script>
<script src="../react-with-addons.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
<style>
.cmnt
{
height: 300px;
width: 300px;
background: lavender;
}
</style>
</head>
<body>
<div id="example"></div>
<script type="text/babel">
var totalCandidates = {'A':'23/10/2017','B':'24/10/2017','C':'23/10/2017'};
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
showComponent: false,
};
}
_onButtonClick() {
console.log(totalCandidates);
this.setState({
showComponent: true,
});
}
_onCalenderChange() {
console.log(this);
this.refs.calender.blur();
}
render() {
return (
<div>
<table>
<thead>
<tr>
<th>Candidate's name</th>
<th>
<input type="text" name="" id="" />
</th>
<th>
Scheduled interview list
</th>
<th>
<input type="date" name="" id="" ref='calender' onChange={this._onCalenderChange.bind(this)} />
</th>
<th>
<select name="" id=""></select>
</th>
</tr>
</thead>
</table>
<button onClick={this._onButtonClick.bind(this)}>Add new</button>
{this.state.showComponent ? <MyComponent /> : null }
</div>
);
}
}
ReactDOM.render(<MyComponent />,document.getElementById("example"));
</script>
</body>
</html>
【问题讨论】:
标签: javascript reactjs