简单的 Redux-Form 组件 returns undefined
Simple Redux-Form Component returns undefined
刚刚创建此表单作为教程的一部分。但是,每当我提交表单时,我的控制台日志都会给我 undefined
。我做错了什么?
import React, {Component} from 'react';
import { reduxForm } from 'redux-form';
class Signin extends Component {
handleFormSubmit({email, password}) {
console.log(email); // this gives me 'undefined'
}
render() {
const {handleSubmit, fields: {email, password}} = this.props;
return (
<form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
<fieldset className="form-group">
<label>Email:</label>
<input {...email} className="form-control" />
</fieldset>
<fieldset className="form-group">
<label>Password:</label>
<input {...password} className="form-control" />
</fieldset>
<button action="submit" className="btn btn-primary">Sign in</button>
</form>
);
}
}
export default reduxForm({
form: 'signin',
fields: ['email', 'password']
})(Signin);
这看起来像 Grider 教程。我用这种方式修复了它,使用了一个接受该字段的 renderInput 函数。不确定这是否对您有用。
import React, {Component} from 'react';
import { reduxForm } from 'redux-form';
import { Field } from 'redux-form';
const renderInput = field => (
<div>
<input {...field.input} type={field.type} className="form-control" />
{field.meta.touched && field.meta.error}
<span>{field.meta.error}</span>
</div>
);
class Signin extends Component {
handleFormSubmit({email, password}) {
console.log(email);
console.log("Hi");
}
render() {
const {handleSubmit, fields: {email, password}} = this.props;
return (
<form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
<fieldset className="form-group">
<label>Email</label>
<Field
name="email"
component={renderInput}
type="text" />
</fieldset>
<fieldset className="form-group">
<label>Password</label>
<Field
name="password"
component={renderInput}
type="text" />
</fieldset>
<button action="submit" className="btn btn-primary">Sign in</button>
</form>
);
}
}
export default reduxForm({
form: 'signin',
fields: ['email', 'password']
})(Signin);
这是由于对 redux-form 的更新。
我们没有从 this.props
导入 {email, password}
的值,而是使用从 redux-form 导入的 Field
组件。
import { Field, reduxForm } from 'redux-form';
然后用它代替您拥有的 input
标签:
<fieldset className="form-group">
<label>Email:</label>
<Field
name="email"
className="form-control"
component="input"
type="text"
placeholder="Email"
/>
</fieldset>
此输入与 redux-form 的连接现在来自 name
属性 而不是从 this.props
中提取
重要提示:名称必须与字段中的名称相同:[] reduxForm 中定义的数组:
export default reduxForm({
form: 'signin',
fields: ['email', 'password']
})(Signin);
在此基础上,如果您想自定义 Field 使用的组件,您可以非常轻松地定义自己的自定义组件。
您可以定义一个函数,而不是向 Field 的组件 属性 提供字符串:
<div className="form-group">
<label>Last name:</label>
<Field type="text" className="form-control" placeholder="Smith" name="lastName"
component={textField} />
</div>
textField 是从另一个文件导入的:import {textField} from '../../redux_form_elements';
而textField如下:
import React from 'react';
export const textField = (field) => (
<div>
<input className="form-control" {...field.input} type={field.type} placeholder={field.placeholder} />
{field.meta.touched && field.meta.error &&
<label id="basic-error" className="validation-error-label">This field is required.</label>
}
</div>
);
刚刚创建此表单作为教程的一部分。但是,每当我提交表单时,我的控制台日志都会给我 undefined
。我做错了什么?
import React, {Component} from 'react';
import { reduxForm } from 'redux-form';
class Signin extends Component {
handleFormSubmit({email, password}) {
console.log(email); // this gives me 'undefined'
}
render() {
const {handleSubmit, fields: {email, password}} = this.props;
return (
<form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
<fieldset className="form-group">
<label>Email:</label>
<input {...email} className="form-control" />
</fieldset>
<fieldset className="form-group">
<label>Password:</label>
<input {...password} className="form-control" />
</fieldset>
<button action="submit" className="btn btn-primary">Sign in</button>
</form>
);
}
}
export default reduxForm({
form: 'signin',
fields: ['email', 'password']
})(Signin);
这看起来像 Grider 教程。我用这种方式修复了它,使用了一个接受该字段的 renderInput 函数。不确定这是否对您有用。
import React, {Component} from 'react';
import { reduxForm } from 'redux-form';
import { Field } from 'redux-form';
const renderInput = field => (
<div>
<input {...field.input} type={field.type} className="form-control" />
{field.meta.touched && field.meta.error}
<span>{field.meta.error}</span>
</div>
);
class Signin extends Component {
handleFormSubmit({email, password}) {
console.log(email);
console.log("Hi");
}
render() {
const {handleSubmit, fields: {email, password}} = this.props;
return (
<form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
<fieldset className="form-group">
<label>Email</label>
<Field
name="email"
component={renderInput}
type="text" />
</fieldset>
<fieldset className="form-group">
<label>Password</label>
<Field
name="password"
component={renderInput}
type="text" />
</fieldset>
<button action="submit" className="btn btn-primary">Sign in</button>
</form>
);
}
}
export default reduxForm({
form: 'signin',
fields: ['email', 'password']
})(Signin);
这是由于对 redux-form 的更新。
我们没有从 this.props
导入 {email, password}
的值,而是使用从 redux-form 导入的 Field
组件。
import { Field, reduxForm } from 'redux-form';
然后用它代替您拥有的 input
标签:
<fieldset className="form-group">
<label>Email:</label>
<Field
name="email"
className="form-control"
component="input"
type="text"
placeholder="Email"
/>
</fieldset>
此输入与 redux-form 的连接现在来自 name
属性 而不是从 this.props
重要提示:名称必须与字段中的名称相同:[] reduxForm 中定义的数组:
export default reduxForm({
form: 'signin',
fields: ['email', 'password']
})(Signin);
在此基础上,如果您想自定义 Field 使用的组件,您可以非常轻松地定义自己的自定义组件。
您可以定义一个函数,而不是向 Field 的组件 属性 提供字符串:
<div className="form-group">
<label>Last name:</label>
<Field type="text" className="form-control" placeholder="Smith" name="lastName"
component={textField} />
</div>
textField 是从另一个文件导入的:import {textField} from '../../redux_form_elements';
而textField如下:
import React from 'react';
export const textField = (field) => (
<div>
<input className="form-control" {...field.input} type={field.type} placeholder={field.placeholder} />
{field.meta.touched && field.meta.error &&
<label id="basic-error" className="validation-error-label">This field is required.</label>
}
</div>
);