React组件中不能获取refs的值,页面报错提示:Uncaught TypeError: Cannot read property 'refs' of null,代码如下:
import React from 'react';
import $ from 'jquery'
import '../app.scss';
export default class MyForm extends React.Component {
submitHandler (event) {
event.preventDefault();
console.log(this.refs.helloTo);
var helloTo = this.refs.helloTo.value;
alert(helloTo);
}
render () {
return (
<form onSubmit={this.submitHandler}>
<input ref='helloTo' type='text' defaultValue='Hello World! ' />
<button type='submit'>Speak</button>
</form>
)
}
}