代码之家  ›  专栏  ›  技术社区  ›  cassmtnr Shannon

以天然形态与福尔米克反应,不发射手榴弹

  •  0
  • cassmtnr Shannon  · 技术社区  · 6 年前

    我正在使用Formik在React原生应用程序中构建表单。

    handleSubmit 当我点击按钮时:

    <ButtonLoading
        loading={isLoading || isSubmitting}
        label="Salvar"
        style={styles.button}
        onPress={handleSubmit}
    />
    

    以下是我填写此表格的完整代码:

    import React, { Component, Fragment } from 'react';
    import { View, ScrollView } from 'react-native';
    import { withFormik } from 'formik';
    
    class Form extends Component {
        state = {
            isCepChecked: false,
            isLoading: false,
            isNewAddress: true,
        };
    
        onChangeCep = () => {
            // Not related to the problem
        };
    
        render() {
            const { isCepChecked, isLoading } = this.state,
                {
                    values,
                    errors,
                    touched,
                    setFieldValue,
                    setFieldTouched,
                    handleSubmit,
                    isSubmitting,
                } = this.props;
    
            return (
                <View style={styles.container}>
                    <ScrollView style={styles.formContainer}>
                        {!isCepChecked ? (
                            <Fragment>
                                <View style={styles.lineContent}>
                                    <InputComponent
                                        label="Digite o CEP"
                                        name="nrCepPre"
                                        value={values.nrCepPre}
                                        error={errors.nrCepPre}
                                        touched={touched.nrCepPre}
                                        onTouch={setFieldTouched}
                                        onChange={setFieldValue}
                                        keyboardType="phone-pad"
                                        mask={'[00000]-[000]'}
                                    />
                                </View>
    
                                <View style={styles.lineContent}>
                                    <ButtonLoading
                                        isLoading={isLoading || isSubmitting}
                                        label="Avançar"
                                        style={styles.button}
                                        onPress={() => this.onChangeCep()}
                                    />
                                </View>
                            </Fragment>
                        ) : (
                            <Fragment>
                                <View style={styles.lineContent}>
                                    <InputComponent
                                        label="CEP"
                                        value={values.nrCep}
                                        mask={'[00000]-[000]'}
                                        editable={false}
                                    />
                                </View>
                                <View style={styles.lineContent}>
                                    <InputComponent
                                        label="Identificação"
                                        name="dsEndereco"
                                        value={values.dsEndereco}
                                        error={errors.dsEndereco}
                                        touched={touched.dsEndereco}
                                        onTouch={setFieldTouched}
                                        onChange={setFieldValue}
                                    />
                                </View>
    
                                <View style={styles.lineContent}>
                                    <ButtonLoading
                                        loading={isLoading || isSubmitting}
                                        label="Salvar"
                                        style={styles.button}
                                        onPress={handleSubmit}
                                    />
                                </View>
                            </Fragment>
                        )}
                    </ScrollView>
                </View>
            );
        }
    }
    
    export default withFormik({
        mapPropsToValues: (props) => ({
            nrCepPre: '',
            dsEndereco: '',
            nrCep: '',
        }),
    
        validationSchema: enderecoSchema,
    
        handleSubmit(values, customObject, bag) {
            console.warn('handle');
        },
    })(Form);
    1 回复  |  直到 6 年前
        1
  •  0
  •   Nader Samadyan    6 年前

    为什么不包括你的 handleSubmit() 而不是通过将其定义为 _hanlderSubmit = (e) = {...} 这样就不需要装订了。那就叫它 this._handleSubmit .

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions