React Form组件onSubmit处理程序不起作用

React Form组件onSubmit处理程序不起作用,第1张

React Form组件onSubmit处理程序不起作用

React
与之一起使用时,
ES2015 classes
应设置
this
事件处理程序

class Form extends React.Component {  constructor(props) {     super(props);     this.handleSubmit = this.handleSubmit.bind(this);  }  handleSubmit(e) {    e.preventDefault();    let loginInput = this.refs.login;    this.props.addCard(loginInput.value);    loginInput.value = '';  }  render() {    return(      <form onSubmit={ this.handleSubmit }>        <input placeholder="githug login" ref="login" />        <button>Add Login</button>      </form>    );  }}

Example

No Autobinding

方法遵循与常规ES6类相同的语义,这意味着它们不会自动将其绑定到实例。您必须显式使用.bind(this)或箭头函数=>。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5642391.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存