redux表单initialValues不更新状态

时间:2018-04-05 05:43:28

标签: reactjs redux redux-form

我尝试在redux-form中设置我的值,但输入没有显示值。有人可以帮帮我吗?

这是我的代码。

import React from "react";
import { Field, reduxForm } from "redux-form";
import {connect} from 'react-redux';
import {renderTextField, renderField, validate, warn} from "../../../Components/Forms/renders"
class SyncValidationForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      errors: {}
    }
  }

  render() {
  const { handleSubmit, pristine, reset, submitting } = this.props;
  return (
    <form onSubmit={handleSubmit}>
      <div className="col-sm-12">
        <h4 className="info-text"> Let's start with the basic details</h4>
      </div>
      <Field name="what.title.value" type="text" component={renderField} label="Titel" />

    </form>
  );
}
}
SyncValidationForm = reduxForm({
  form: 'insertstart',
  enableReinitialize: true,
  validate, // <--- validation function given to redux-form
  warn
})(SyncValidationForm);

const mapStateToProps = state => {
  return {
    initialValues: state.items.item,

  }
}

export default connect(mapStateToProps)(SyncValidationForm)

我的控制台日志我通过console.log(this.props);

看到了这一点

initialValues:{what.title.value:“test”}

2 个答案:

答案 0 :(得分:1)

您需要将初始值作为对象传递,因此不是

what.title.value: "test"

您的state.items.item应如下所示:

{ what: { title: { value: "test" } } }

有关工作示例,请参阅here

答案 1 :(得分:1)

始终使用change函数绑定redux-form字段

中的数据
componentWillMount() {
    const { change } = this.props
    change('basicDetails', 'sdfuhdsuiafghfudsauifhdsuifhuiads') // this will bind name with basicDetails
}

<form onSubmit={handleSubmit}>
      <div className="col-sm-12">
        <h4 className="info-text"> Let's start with the basic details</h4>
      </div>
      <Field name="basicDetails" type="text" component={renderField} label="Titel" />
    </form>