initialValues 未以 redux 形式加载

initialValues not loading in redux-form

我可能用错了,但我正在尝试获取一个 redux 表单来加载 initialValues,但它不起作用。我有一个呈现表单的容器组件:

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() 方法包含一个加载简单组件的字段。请注意,它是 name 的单个名称字段。我正在以通常的方式初始化 reduxForm,但是当屏幕加载时,表单字段未填充预期的 "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

中看不到

我认为您退回了错误的地方。 试试下面的代码片段:

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