在我的react应用程序中,在触发onBlur时执行以下脚本。但是e.target返回null。因此,我无法检索输入的文本进行验证。

handleNameValidation({ target }) {
    console.log("The target is : ", target);
}


但是,当触发onChange事件时,我有一个类似的脚本可以正常工作。
这是预期的行为还是触发onBlur时如何访问该值。

任何指针都会有所帮助。

感谢致敬,

最佳答案

看来您失去了处理程序的上下文:

1)使用类属性字段

handleNameValidation = ({ target }) => {
    console.log("The target is : ", target);
}


2)在contructor中绑定上下文

construtctor(props){
  super(props);

  this.handleNameValidation = this.handleNameValidation.bind(this);
}

handleNameValidation({ target }) {
    console.log("The target is : ", target);
}

10-06 04:27
查看更多