railsuser400
railsuser400

Reputation: 1023

initialValues not loading in redux-form

I'm probably using this wrong but I'm trying to get a redux form to load with initialValues which isn't working. I've got a container component which renders a form:

class ProductScreen extends Component {
  render() {
    return (
      <ProductForm {...this.props} {...this.state} />
    )
  }
}

const mapStateToProps = (state) => {
  return {
    initialValues: {name: 'Ferrari'}
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(ProductScreen)

For the ProductForm component the render() method includes a Field which loads a simple component. Note that it's a single field of name being name. I'm initialising the reduxForm the usual way but when the screen loads the form field isn't being populated with the expected "Ferrari" value:

// importing functions from redux-form
import { Field, reduxForm } from 'redux-form/immutable'

// inside the render() function
<Field component={renderInput} name='name' ref='name' placeholder='Product name' />

const ProductForm = reduxForm({
  form: 'createProduct',
  enableReinitialize: true
})(ProductFormComponent)

const renderInput = ({input, ...inputProps}) => {
  return (
    <TextInput {...inputProps} {...input} />)
}

In the React native debugger if I console out the renderInput function I can see the 'Ferrari' value in inputProps.meta.initial but not in input.value

Upvotes: 1

Views: 532

Answers (1)

Yogesh Chuahan
Yogesh Chuahan

Reputation: 358

I think you are returning it at wrong place. Try the following code snippet:

const ProductForm = reduxForm({
  form: 'createProduct',
  initialValues: {name: 'Ferrari'},
  enableReinitialize: true
})(ProductFormComponent)

Upvotes: 1

Related Questions