use*_*066 28 html onblur reactive-programming reactjs react-jsx
我正在尝试创建一个密码确认功能,只有在用户离开确认字段后才会出现错误.我正在使用Facebook的React JS.这是我的输入组件:
<input
type="password"
placeholder="Password (confirm)"
valueLink={this.linkState('password2')}
onBlur={this.renderPasswordConfirmError()}
/>
Run Code Online (Sandbox Code Playgroud)
这是renderPasswordConfirmError:
renderPasswordConfirmError: function() {
if (this.state.password !== this.state.password2) {
return (
<div>
<label className="error">Please enter the same password again.</label>
</div>
);
}
return null;
},
Run Code Online (Sandbox Code Playgroud)
当我运行页面时,输入冲突的密码时不会显示该消息.
Jar*_*yth 41
这里有一些问题.
1:onBlur期望回调,并且您正在调用renderPasswordConfirmError并使用返回值,该值为null.
2:你需要一个地方来渲染错误.
3:你需要一个标志来跟踪"我验证",你会在模糊时设置为true.如果需要,可以在焦点上将其设置为false,具体取决于您所需的行为.
handleBlur: function () {
this.setState({validating: true});
},
render: function () {
return <div>
...
<input
type="password"
placeholder="Password (confirm)"
valueLink={this.linkState('password2')}
onBlur={this.handleBlur}
/>
...
{this.renderPasswordConfirmError()}
</div>
},
renderPasswordConfirmError: function() {
if (this.state.validating && this.state.password !== this.state.password2) {
return (
<div>
<label className="error">Please enter the same password again.</label>
</div>
);
}
return null;
},
Run Code Online (Sandbox Code Playgroud)