Wouter
Wouter

Reputation: 437

redux form initialValues not updating state

I try to set my values in my redux-form, but the input doesn't show the value. Can somebody please help me?

This is my code.

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)

I my console log I see this by console.log(this.props);

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

Upvotes: 0

Views: 1201

Answers (2)

Ashh
Ashh

Reputation: 46441

Always use change function to bind data in redux-form fields

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>

Upvotes: 1

Dario
Dario

Reputation: 6270

You need to pass the initial values as an object, so instead of

what.title.value: "test"

your state.items.item should look like:

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

See here for a working example.

Upvotes: 1

Related Questions