InitialValues工作正常,但更新一个字段时将其他字段设置为空

时间:2019-06-01 05:02:49

标签: reactjs react-native react-redux redux-form

我有一个工作正常的redux-form组件,并分配了initialValues来让我的用户更新其现有数据。但是我得到的是,当我更新一个字段时,另一个字段将为空。

我正在使用“ redux”:“ ^ 4.0.1”,“ redux-form”:“ ^ 8.2.0”

在CustomerScreen.js中

<CustomerForm 
    onSubmit={values => this.onSubmitForm(values)}
    isLoading={this.state.formLoading}
    initialValues={customer}
/>

在CustomerForm.js

const CUSTOMER_FIELDS = [
    { name: 'name', label: 'Full Name' },
    { name: 'email', label: 'Email Address', keyboardType: 'email-address', isRequired: true },
    { name: 'phone', label: 'Phone Number', keyboardType: 'phone-pad' },
    { name: 'address', label: 'Address', keyboardType: 'default' }
]

let CustomerForm = ({ onSubmit, isLoading, handleSubmit, ...rest }) => {
    const styles = getStyleSheet('form', rest.isDarkTheme)

    return (
        <View style={styles.container}>
            <View style={styles.fieldsWrapper}>
                <FormRender fields={CUSTOMER_FIELDS} />
            </View>

            <View style={styles.buttonWrapper}>
                <Button
                    type="primary" 
                    activeStyle={styles.buttonActiveStyle} 
                    size="large"
                    loading={isLoading}
                    style={styles.button}
                    onPress={handleSubmit(values => onSubmit(values))}
                >
                    SAVE
                </Button>
            </View>
        </View>
    )
}

function validate(values) {
    const error = {}
    _.each(CUSTOMER_FIELDS, ({ name, label, isRequired }) => {
        if (isRequired) {
            if (values[name] === undefined || isEmpty(values[name])) {
                error[name] = `${label} is required fields!`
            }
        }
    })
    return error
}

function mapStateToProps({ isDarkTheme }) {
    return { isDarkTheme }
}

CustomerForm = reduxForm({
    validate,
    form: 'CustomerForm'
})(CustomerForm)

CustomerForm = connect(mapStateToProps)(CustomerForm)

export default CustomerForm

我希望输出仅在我正在处理的字段上更新,而其他字段仍保持其原始值。

0 个答案:

没有答案