[英]React JS: Reusable components
我已经用这样的结构创建了表单验证
var Signin = React.createClass({
render: function() {
return (
<Form>
<Input type="text" name="email" labelName="Email" rules="isEmail" error:"Email not valid" />
<Input type="password" name="password" labelName="Password" rules="isLength:6" error:"Passowrd not valid"/>
</Form>
);
}
});
因为,例如,“电子邮件”输入将在应用程序的不同部分使用,所以我避免每次都添加相同的属性(名称,类型,labelName,规则和错误)。 所以我会创建这样的东西
var InputEmail = React.createClass({
render: function () {
return (
<Input type="text" name="email" labelName="Email" rules="isEmail" error="Email not valid"/>
)
}
});
var InputPassword = React.createClass({
render: function () {
return (
<Input type="password" name="password" labelName="Password" rules="isLength:6" error="Passwordnot valid"/>
)
}
});
因此登录组件应为
var Signin = React.createClass({
render: function() {
return (
<Form>
<InputEmail />
<InputPassword />
</Form>
);
}
});
但是这样,我会遇到两个错误:
我无法在Form中找到Input的props.name,因为InputEmail中没有。
在Input的呈现函数中,状态为null
如何创建可重用/继承的组件? 我无法同时使用合成模式和mixins
我添加了完整的代码:表格
var Form = React.createClass({
getInitialState: function () {
return {
isValid : false,
isSubmitting: false
}
},
componentWillMount: function(){
this.model = {};
this.inputs = {};
this.registerInputs(this.props.children);
},
registerInputs: function(children){
React.Children.forEach(children, function (child) {
if (child.props.name) {
child.props.attachToForm = this.attachToForm;
child.props.detachFromForm = this.detachFromForm;
child.props.validate = this.validate;
}
if (child.props.children) {
this.registerInputs(child.props.children);
}
}.bind(this));
},
attachToForm: function (component) {
this.inputs[component.props.name] = component;
this.model[component.props.name] = component.state.value;
this.validate(component);
},
detachFromForm: function (component) {
delete this.inputs[component.props.name];
delete this.model[component.props.name];
},
validate: function (component) {
var isValid = true;
// validation code
component.setState({
isValid: isValid,
}, this.validateForm);
},
validateForm: function () {
var formIsValid = true;
var inputs = this.inputs;
Object.keys(inputs).forEach(function (name) {
if (!inputs[name].state.isValid) {
formIsValid = false;
}
});
this.setState({
isValid: formIsValid
});
},
updateModel: function (component) {
Object.keys(this.inputs).forEach(function (name) {
this.model[name] = this.inputs[name].state.value;
}.bind(this));
},
submit: function (event) {
event.preventDefault();
this.setState({
isSubmitting : true
});
this.updateModel();
console.log(this.model);
},
render: function () {
return (
<form className="ui form" onSubmit={this.submit}>
{this.props.children}
<button className="ui button" type="submit" disabled={this.state.isSubmitting}>Accedi</button>
</form>
);
}
});
输入
var Input = React.createClass({
getInitialState: function(){
return {
value : this.props.value || "",
isValid: true
}
},
setValue: function (event) {
this.setState({
value: event.target.value
}, function () {
this.props.validate(this);
}.bind(this));
},
componentWillMount: function () {
if (this.props.required) {
this.props.validations = this.props.validations ? this.props.validations + ',' : '';
this.props.validations += 'isLength:1';
}
// ERROR: TypeError: this.props.attachToForm is not a function
this.props.attachToForm(this);
},
componentWillUnmount: function () {
this.props.detachFromForm(this);
},
render: function () {
var className = "field";
if(this.props.className){
className += " " + this.props.className;
}
if(this.props.required){
className += " required";
}
var Label;
if(this.props.labelName){
Label = (<label htmlFor={this.props.name}>{this.props.labelName}</label>);
}
var Error;
if(!this.state.isValid){
Error = (<div className="ui">{this.props.error || this.props.name + " not valid"}</div>);
};
return (
<div className={className}>
{Label}
<input type={this.props.type || "text"} id={this.props.name} name={this.props.name} onChange={this.setValue} value={this.state.value} />
{Error}
</div>
);
}
});
有了这个作品
ReactDOM.render(
<Form>
<Input type="text" name="email" labelName="Email" rules="isEmail" error:"Email not valid"/>
</Form>,
document.getElementById('app')
);
这样,我得到:“ TypeError:this.props.attachToForm不是函数this.props.attachToForm(this);”
ReactDOM.render(
<Form>
<InputEmail/>
</Form>,
document.getElementById('app')
);
PS:我试图在jsfiddle上添加此代码,但得到“!TypeError:无法定义属性“ attachToForm”:对象不可扩展”
设置有两个主要问题:
<Form>
是以这样的方式设置的,即该表单的子级需要有道具,否则它将不起作用。 <InputEmail>
包装器不完整。 它需要将所有道具传递到<Input>
,包括向下传递的Form函数。 广告1:修正表格,以确保添加了验证方法
出现错误的原因是<Form>
子代需要具有props.name。 然后,通过将窗体的功能(包括attachToForm)添加到子级中来注册它们。 这是在方法registerInputs()
。
在原始变体中, <Input>
组件具有道具,因此一切顺利。
在改编的变体中,包装器<InputEmail>
不再具有道具,因此, attachToForm()
和其他函数不会添加到道具,并且当<Input>
尝试调用该函数时会出现错误。
解决此问题的最简单方法:在render函数中添加至少1个prop,然后在registerInputs()
检查,例如:
ReactDOM.render(
<Form>
<InputEmail initialValue={'name@domain.com'}/>
</Form>,
document.getElementById('app')
);
然后在registerInputs()
,更改行:
if (child.props.name) {
至:
if (child.props.initialValue) {
2.扩展<InputEmail>
包装器以传递函数
最简单的方法是添加{...this.props}
,如下所示:
var InputEmail = React.createClass({
render: function () {
return (
<Input {...this.props}
type="text" name="email" labelName="Email" rules="isEmail" error="Email not valid"/>
)
}
});
这样,由<Form>
传递给<InputEmail>
组件的函数(以及任何其他道具)将被传递给<Input>
组件。
PS: registerInputs()
用于检查child.props.children的代码无法正常工作:在调用它时, <InputEmail>
组件没有子代。 顾名思义,它检查作为道具传递下来的孩子。 唯一通过的道具是initialValue
。
作为次要问题,我建议再进行2处更改:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.