Reactjs 为什么redux表单返回字符串而不是对象?
我正在尝试访问redux表单上的touch属性,但由于某种原因,当我打印字段道具时,我只使用值而不是对象。我错过了什么Reactjs 为什么redux表单返回字符串而不是对象?,reactjs,redux-form,Reactjs,Redux Form,我正在尝试访问redux表单上的touch属性,但由于某种原因,当我打印字段道具时,我只使用值而不是对象。我错过了什么 import { reduxForm, Field } from 'redux-form'; render() { const { fields: { email, phone }, handleSubmit } = this.props; console.log(email) //prints just the value "email" instead of th
import { reduxForm, Field } from 'redux-form';
render() {
const { fields: { email, phone }, handleSubmit } = this.props;
console.log(email) //prints just the value "email" instead of the field object with the touched method, etc. When I do console.log(email.touched) I get undefined error.
return (
<form onSubmit={handleSubmit(this.onSubmit)}>
<Field name="email" component="input" type="email" { ...email } />
<Field name="phone" component="input" type="number" { ...phone } />
</form>
);
}
export default ReduxFormTest = reduxForm({
form: 'uniqueForm',
fields: ['email', 'phone']
})(TestClass);
从'redux form'导入{reduxForm,Field};
render(){
const{fields:{email,phone},handleSubmit}=this.props;
console.log(email)//仅打印值“email”,而不是带有touch方法的字段对象,等等。当我执行console.log(email.touch)时,会出现未定义的错误。
返回(
);
}
导出默认ReduxFormTest=reduxForm({
表格:'唯一表格',
字段:[“电子邮件”、“电话”]
})(测试类);
从v5到v6,redux表单发生了突破性的变化。以前你可以做一些类似的事情,你必须访问触摸场。如果您想做类似的事情来查看字段上是否有错误,您需要创建自己的组件来传递给redux表单的字段组件
您的自定义组件
const CustomComponent = function(field) {
return (
<div>
<input
type={field.type}
{...field.input}
/>
{field.meta.touched && field.meta.error && <div className='error'>{field.meta.error}</div>}
</div>
)
}
<Field name="my-prop" component={CustomComponent} />
const CustomComponent=函数(字段){
返回(
{field.meta.com&&field.meta.error&&{field.meta.error}
)
}
然后将其与字段组件一起使用
const CustomComponent = function(field) {
return (
<div>
<input
type={field.type}
{...field.input}
/>
{field.meta.touched && field.meta.error && <div className='error'>{field.meta.error}</div>}
</div>
)
}
<Field name="my-prop" component={CustomComponent} />
也来看看迁移,希望这有帮助 您将
v5
语法与v6
语法混淆了。在v6
中,装饰过的表单组件不再传递this.props.fields
。重新阅读迁移指南,就像@tyler iguchi所说的。这样做有效,唯一的问题是当我在输入中键入一个字符时,它变为非活动状态,我必须再次单击以继续键入。我看到您编辑了field.meta.toucted行,但我还没有添加该行,因此问题不在该行中。上述问题仅发生在我尝试键入的第一个字段中。const CustomComponent是在render方法中定义的还是我必须将其放入自己的类中?