【问题标题】:How to useFormik pass Props?如何使用Formik pass Props?
【发布时间】:2022-01-18 08:16:28
【问题描述】:

将 Reactjs 与打字稿一起使用

我想将 useFormik 钩子传递给组件 props。

这样做的原因是为了减少不必要的行并增加重用。

我当前的代码

...
const formik = useFormik({
    initialValues: { userName: ''},
    validationSchema,
    onSubmit: (values) => {}
})

return (
  <Form>
    {/* A place to make a component. */}
    <Text
       id="userName"
       fullWidth
       label="Name"
       defaultValue={formik.values.userName}
       onChange={formik.handleChange}
       onBlur={formik.handleBlur}
       error={formik.touched.userName && Boolean(formik.errors.userName)}
       helperText={formik.touched.userName && formik.errors.userName}
    >
    {/* A place to make a component. */}
  </Form>
)

自定义组件,这是问题的重点。


interface props {
    id: string;
    formik : what, // How do I deliver the prop here?
  }

const TextFieldCustom = ({ id, formik }: props) => {
    return (
     <Text
         id={id}
         fullWidth
         label={id}
         defaultValue={formik.values.userName}
         onChange={formik.handleChange}
         onBlur={formik.handleBlur}
         error={formik.touched.userName && Boolean(formik.errors.userName)}
         helperText={formik.touched.userName && formik.errors.userName}
    >

  );
};

由于您的回答,我的代码完成了。

...
const formik = useFormik({
    initialValues: { userName: ''},
    validationSchema,
    onSubmit: (values) => {}
})

return (
  <Form>
    {/* A place to make a component. */}
    <TextFieldCustom id="username" formik={formik}/>
    {/* A place to make a component. */}
  </Form>
)

我想要你的好解决方案。

【问题讨论】:

    标签: reactjs typescript formik


    【解决方案1】:

    如果你想从子组件访问 formik 助手,你可以使用 useFormikContext。我认为这更容易。

    根据您的代码,我还建议根据需要使用 Formik 组件作为 Form 组件的父组件,但这可能是另一回事。

    这是我要做的(请注意,我已经用表单 html 标记替换了表单组件):

    父组件:

    export interface IFormData {
      username: string;
    }
    
    const validationSchema = Yup.object().shape({
      userName: Yup.string()
        .required("Username required"),
    });
    
    const ParentComponent = () => {
      const formikConfig = useFormik({
        initialValues: { userName: "" },
        validationSchema,
        onSubmit: (values) => {},
      });
    
      return (
        <form onSubmit={formikConfig.handleSubmit}>
          <TextCompo id="id" />
        </form>
      );
    };
    
    export default ParentComponent;
    

    子组件:

    interface ITextCompoProps {
      id: string;
    }
    
    const TextFieldCustom = (props: ITextCompoProps) => {
      const { id } = props;
      const context = useFormikContext<IFormData>();
    
      return (
        <Text
          id={id}
          fullWidth
          label={id}
          defaultValue={context.values.userName}
          onChange={context.handleChange}
          onBlur={context.handleBlur}
          error={!!(context.errors.username && context.touched.username)}
          helperText={context.touched.username && context.errors.username}
        />
      );
    };
    
    export default TextFieldCustom;
    

    【讨论】:

      猜你喜欢
      • 2021-03-08
      • 2021-04-07
      • 2021-01-03
      • 1970-01-01
      • 2021-01-30
      • 2019-08-03
      • 2022-07-14
      • 2018-09-06
      • 2022-01-06
      相关资源
      最近更新 更多