【发布时间】:2016-01-30 05:23:48
【问题描述】:
我有以下 React 组件:
class Form extends React.Component {
handleSubmit(e) {
e.preventDefault();
let loginInput = ReactDOM.findDOMNode(this.refs.login);
// change Main component's state
this.props.addCard(loginInput.value);
// reset the form
loginInput.value = '';
}
render() {
return(
<form onSubmit={this.handleSubmit}>
<input placeholder="githug login" ref="login" />
<button>Add Login</button>
</form>
);
}
}
如您所见,我希望在提交表单时调用handleSubmit 函数。我通过将函数添加到onSubmit 处理程序来表明这一点。
函数在正确的时间被调用。但是,该函数中this 的值是null。这让我感到惊讶,因为我预计 this 的值会成为 React 组件。 this 为 null 的事实令我感到惊讶,因为我使用的逻辑/代码与 official React documentation 建议的非常相似。
如果能帮助我弄清楚为什么 this 不是预期的 React 组件,以及如何修复代码以使其成为,我将不胜感激。
【问题讨论】:
标签: reactjs