我的 render
函数中有一个简单的表单,如下所示:
render : function() {
return (
<form>
<input type="text" name="email" placeholder="Email" />
<input type="password" name="password" placeholder="Password" />
<button type="button" onClick={this.handleLogin}>Login</button>
</form>
);
},
handleLogin: function() {
//How to access email and password here ?
}
我应该在我的 handleLogin: function() { ... }
中写什么来访问 Email
和 Password
字段?
原文由 myusuf 发布,翻译遵循 CC BY-SA 4.0 许可协议
使用输入上的
change
事件来更新组件的状态并在handleLogin
中访问它:工作 小提琴。
另外,阅读文档,有一整节专门用于处理表单: 表单
以前你也可以使用 React 的双向数据绑定助手 mixin 来实现相同的目的,但现在它已被弃用,取而代之的是设置值和更改处理程序(如上):
文档在这里: 双向绑定助手。