首页 > 解决方案 > 即使未显示,字段仍会发布

问题描述

根据变量的真实性显示字段:

const [paymentCash, setPaymentCash] = useState(null);
...
const onSubmit = async (values) => {
        try {
            const response = await createGroupService(values);
            onSuccess(_getText('notif.msg.group.created.ok'));
        } catch (error) {
            hideLoader();
        }
    };
<Formik
            initialValues={isUpdate ? updateFormValues : initialValues}
            validationSchema={validationSchema}
            onSubmit={onSubmit}
        >
            {(formik) => {
                    ...
                    <FormikControl
                        formik={formik}
                        control="single-select"
                        label={_getText("billing.model")}
                        name="billingModel"
                        options={billingModels}
                        onChangeEffect={(inputValue) => {
                                                            setPaymentCash(inputValue.code == "CASH" ? true : false);
                                                        }
                                        }
                    />
...
{paymentCash && <FormikControl
                            control="input"
                            label={_getText("prix.achat")}
                            name="purchasePrice"
                            placeholder={_getText("prix.achat")}
                            type="number"
                        />}
...
<Button text="Submit" type="submit" />

当 paymentCash 为 false 并且提交表单时,我仍然会收到字段 purchasePrice 。那么如何解决呢?

标签: reactjs

解决方案


推荐阅读