【发布时间】:2017-05-23 16:36:35
【问题描述】:
我正在使用 6.7.0 版的 redux-form,一旦 Axios 通过“fetchPerson”redux 操作成功执行 http get 请求,我就无法初始化我的表单。 当我导出 ReduxForm 时,我已经设置了“enableReinitialize:true”,但它仍然没有呈现结果。 (顺便说一句,验证函数也没有被调用)
这里是我的 reducer 的全部代码:
import React, { Component } from 'react';
import { Field, reduxForm } from 'redux-form';
import { fetchPerson, editPerson } from '../actions';
import { connect } from 'react-redux';
import { Link} from 'react-router-dom';
import 'react-table/react-table.css';
class EditPerson extends Component {
componentDidMount() {
const { id } = this.props.match.params;
this.props.fetchPerson(id);
}
renderField(field) {
const { meta: { touched, error } } = field;
const className = `form-group ${touched && error ? 'has-danger' : ''}`;
return (
<div className={className}>
<label>{field.label}</label>
<input
className="form-control"
type="text"
{...field.input}
/>
<div className="text-help">
{touched ? error : ''}
</div>
</div>
);
}
onSubmit(values) {
const { id } = this.props.match.params;
this.props.editPerson(id,values, () => {
this.props.history.push('/personas');
});
}
render() {
const { handleSubmit } = this.props;
const { person } = this.props;
if (!person) {
return <div>Loading...</div>;
}
console.log("this.props");
console.log(this.props);
return (
<div className="row">
<div className="col-lg-12">
<div className="row wrapper border-bottom white-bg page-heading">
<div className="col-lg-10">
<h2>Editar Persona {person.name}</h2>
<ol className="breadcrumb">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="personas/">Personas</a>
</li>
<li className="active">
<strong>Editar Persona :{person.name}</strong>
</li>
</ol>
</div>
</div>
</div>
<div className="wrapper wrapper-content animated fadeInRight">
<div className="row">
<div className="col-lg-6">
<div className="row">
<div className="col-lg-12">
<div className="well" style={{background: 'white'}}>
<h3>Datos</h3>
<form onSubmit={handleSubmit(this.onSubmit.bind(this))}>
<Field
label="Nombre"
name="name"
component={this.renderField}
/>
<Field
label="Email"
name="email"
component={this.renderField}
/>
<Field
label="Telefono"
name="tel"
component={this.renderField}
/>
<button type="submit" className="btn btn-primary">Submit</button>
<Link to="/personas" className="btn btn-danger">Cancel</Link>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
)
}
}
function validate(values) {
// console.log(values) -> { title: 'asdf', categories: 'asdf', content: 'asdf' }
const errors = {};
// Validate the inputs from 'values'
if (!values.name) {
errors.title = "Enter a name";
}
if (!values.email) {
errors.categories = 'Enter some email';
}
if (!values.tel) {
errors.content = 'Enter some content telefono';
}
return errors;
}
function mapStateToProps(state) {
console.log("state mapStateToProps");
console.log(state);
return {
person: state.personas.person,
initialValues:state.personas.person,
}
};
export default reduxForm({
enableReinitialize : true,
form: 'PersonEditForm'
})(
connect(mapStateToProps,{ editPerson, fetchPerson })(EditPerson)
);
这是我的浏览器控制台,它在第二行中创建了 personas.person:
【问题讨论】:
-
你能展示一个示例角色吗?对于 initialValues,字段上的
name属性和角色对象上的键必须匹配。您还可以尝试创建一个 componentWillReceiveProps 函数来检查您的 initialValues 道具是否正确传递到表单。 -
你好斯蒂芬!谢谢四爷的帮助!当然:persona.person { create_time:"2017-05-21T16:52:28.000Z" email:"hbulgarini@gmail.com" name:"Hector Bulgarini" person_id:63 tel:"293482956" type:null update_time: null }关于componentWillReceiveProps,你能给我举个例子吗?
-
其实你应该可以只展开打印在
this.props下面的对象。检查您的 initialValues 对象是否正在传入,并且属性是否与您的 name 道具匹配。
标签: reactjs redux react-redux redux-form react-redux-form