2017-08-07 40 views
0

我可能使用了這個錯誤,但我試圖獲得一個與initialValues不起作用的加載形式。我有一個容器組件,它呈現一種形式: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通常的方式,但是當屏幕加載表單字段沒有被與預期的「法拉利」值填充:

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

在陣營機調試,如果我安慰了renderInput功能我可以看到inputProps.meta.initial中的'法拉利'值,但不是input.value

回答

1

我認爲你是在錯誤的地方返回它。 請嘗試以下代碼片段:

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