首页 文章

initialValues没有以redux形式加载

提问于
浏览
1

我可能正在使用这个错误,但我正在尝试使用initialValues加载一个redux表单,但这不起作用 . 我有一个容器组件,它呈现一个表单:

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

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

export default connect(mapStateToProps, mapDispatchToProps)(ProductScreen)

对于ProductForm组件, render() 方法包括一个加载简单组件的Field . 请注意,它是一个名为 name 的单个字段 . 我在'm initialising the reduxForm the usual way but when the screen loads the form field isn'中填充了预期的"Ferrari"值:

// 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} />)
}

在React本机调试器中,如果我控制出 renderInput 函数,我可以在 inputProps.meta.initial 中看到'Ferrari'值但在 input.value 中没有

1 回答

  • 2

    我想你是在错误的地方归还它 . 请尝试以下代码段:

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

相关问题