(反应)未捕获的类型错误:使用 ref 时无法读取未定义的属性'this'



我正在尝试使用 React 中的 ref 引用输入字段,但是我收到错误:"未捕获的类型错误:无法读取未定义的属性'this'"。但是,引用是定义的。React 无法在我的代码中找到引用有什么原因吗?

    export default class ContractorSignUp extends Component {

  constructor(props) {
    super(props);
    this.handleSubmit = this.handleSubmit.bind(this);
  }
  handleSubmit(event) {
    event.preventDefault();
    // Find the text field via the React ref
    const user = ReactDOM.this.refs.emailInput.value.trim();
    // Clear form
    ReactDOM.this.refs.emailInput.value = '';
  }

  render() {
    return (
            <form onSubmit={this.handleSubmit.bind(this)}>
              <input className="textinput" type="e-mail" placeholder="E-mail" ref="emailInput">
              </input>
          <button id="formsubmitbutton" onClick={this.handleSubmit.bind(this)}><span>Submit</span></button>
            </form>
                                    )
  }
};

只需删除ReactDOM部分:

// Find the text field via the React ref
const user = this.refs.emailInput.value.trim();
// Clear form
this.refs.emailInput.value = '';

最新更新