【问题标题】:unable to click on button with Formik form无法使用 Formik 表单单击按钮
【发布时间】:2020-08-31 11:16:21
【问题描述】:

我正在使用 Formik 进行验证,如果输入为空,我将收到“必需的错误”。

当我单击表单的按钮时,应呈现一个列表 (UsersFoundList)。此列表位于表单之外,其中包含进一步具有按钮的元素。当我第一次单击列表中的任何这些按钮时,它们不起作用。相反,formik 错误开始出现在输入字段中。

只有在我尝试再次提交表单时才会出现此错误。当我点击其他任何东西时不会。

const [showFlatList, setShowFlatList] = useState(false);

    const handleSubmitForm = (
    values: FormValues,
    helpers: FormikHelpers<FormValues>,
  ) => {
    console.log('hello', values.input)
    setShowFlatList(true);
    helpers.resetForm();
  };
  return (
    <View style={styles.container}>
      <View >
          <Formik
            initialValues={initialValues}
            onSubmit={handleSubmitForm}
            validationSchema={addRelationValidationSchema}>
            {({ handleChange, handleBlur, handleSubmit, values }) => (
              <View style={styles.searchFieldContainer}>
                <View style={styles.form}>
                  <FieldInput
                    handleChange={handleChange}
                    handleBlur={handleBlur}
                    value={values.input}
                    fieldType="input"
                    icon="user"
                    placeholderText="E-Mail oder Telefonnummer oder Name"
                  />
                  <ErrorMessage
                    name="input"
                    render={(msg) => <ErrorText errorMessage={msg} />}
                  />
                </View>
                <View style={styles.buttonContainer}>
                  <NewButton buttonText="Suchen" onPress={handleSubmit} />
                </View>
              </View>
            )}
          </Formik>
        </View>
                <View style={styles.listHolder}>
          {showFlatList && (
            <UsersFoundList/>
          )}
        </View>
    </View>
  );

转载示例: https://snack.expo.io/@nhammad/jealous-beef-jerky

在 Android 手机中很明显。网页版的代码沙盒上没有。

编辑:刚刚意识到不仅是按钮,而且如果我单击屏幕上的任何位置,错误就会再次出现。

【问题讨论】:

  • 如果表单无效,则永远不会调用提供给 formik 的 onSubmit 函数。由于存在验证错误,它会短路。如果您想做某事而不管是否有表单错误,那么您必须将您的 showFlatList 移出 onsubmit 并在用户按下按钮时将其与 handleSubmit 分开调用。
  • 是的,但该列表在表单之外。我单击列表中的一个按钮,而不是单击表单按钮,那么为什么要调用 handleSubmit 呢?请看codesandbox@Adam
  • 当我按下用户列表上的按钮时,表单没有提交,验证错误也没有出现,不知道你的意思?
  • 我也无法重现这个,不过听起来像是一个事件冒泡问题。
  • 在代码框上,它在安卓手机中向我显示错误。你在网上查看@sandrooco

标签: javascript css reactjs react-native formik


【解决方案1】:

在您的表单中,您需要将ref 传递给您的FieldInput 组件


// get a ref for your field input
const fieldRef = useRef();


 const handleSubmitForm = (
    values: FormValues,
    helpers: FormikHelpers<FormValues>,
  ) => {
    // on successful submission blur the field
    fieldRef.current.blur();
    ...
 };

...


   <Form
     ...
     render={() => (
       {/* pass the ref to your fieldinput in your form */}      
       <FieldInput ref={ref} .../>
     )}

   />

然后在您自定义的FieldInput 组件中,您需要像这样转发引用:

import React, { forwardRef } from 'react';

export const FieldInput = forwardRef((props,ref) => {
  return (
    <Item rounded={rounded} style={[styles.inputItem, style]}>
      <Input
         ref={ref}
         ...
      />
    />
  )
});

【讨论】:

  • @Jnl - 没有工作是一个糟糕的描述,但快速浏览一下您的注释代码看起来您​​没有正确实现 forwardRef。 refNOT 道具 - 它作为第二个参数(在道具之后)传递给 forwardRef 组件
  • 抱歉,我不确定你实现 forwardRef 是什么意思。 fieldRef.current.blur();给出 Typescript 错误,因此我尝试使用 if 语句。我通过了这个:ref={fieldRef}。但是应用的行为还是和以前一样
  • 目前,我的 fieldInput 道具是 handleChange、handleBlur、fieldType、placeholderText 等。我应该删除所有这些并仅使用 forwardref 吗?我已经尝试在这里添加 ref。
  • 不知何故,当我单击任何项​​目 (UserFoundEntry) 时,会触发 onBlur 事件。它不应该发生。
猜你喜欢
  • 2021-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
  • 2019-05-30
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多