I use the React to write this demo. I use the Webpack to build this demo.When I start this demo, the error will show me.
The error:
Uncaught
The only warning in your code is due the the fact that you are not extending the correct class, you need to extend React.Component
.
class App extends React.Component {
constructor(props){
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick(){
if(this.myTextInput !=null) {
this.myTextInput.focus();
}
}
render (){
return (
<div>
<input type="text" ref={(ref) => this.myTextInput = ref} />
<input type="button"
value="'Focus the text input"
onClick={this.handleClick}
/>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script>
<div id="app"></div>
try following:
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
class App extends React.Compoment {
constructor(props){
super(props);
this.myTextInput = this.myTextInput.bind(this);
this.handleClick = this.handleClick.bind(this);
}
handleClick(){
if(this.myTextInput !=null) {
this.myTextInput.focus();
}
}
render (){
return (
<div>
<input type="text" ref={(ref) => this.myTextInput = ref} />
<input type="button"
value="'Focus the text input"
onClick={this.handleClick}
/>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('app'));